From 8b704fa66888448e0cfeec4e6dc1208947ee9a0b Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 15 Aug 2024 22:47:49 +0200 Subject: [PATCH] feat: impl so no longer needed from:: Co-authored-by: zaaarf --- src/api/controller.rs | 6 +++++- src/buffer/controller.rs | 4 ++-- src/cursor/controller.rs | 4 ++-- src/ffi/lua.rs | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/api/controller.rs b/src/api/controller.rs index 0a8b990..3170256 100644 --- a/src/api/controller.rs +++ b/src/api/controller.rs @@ -45,8 +45,12 @@ pub trait Controller : Sized + Send + Sync { } } - fn callback(&self, cb: ControllerCallback); + /// registers a callback to be called on receive. + /// + /// there can only be one callback at any given time. + fn callback(&self, cb: impl Into>); + /// clears the currently registered callback. fn clear_callback(&self); /// block until next value is available without consuming it diff --git a/src/buffer/controller.rs b/src/buffer/controller.rs index 2766056..acbf201 100644 --- a/src/buffer/controller.rs +++ b/src/buffer/controller.rs @@ -98,8 +98,8 @@ impl Controller for BufferController { Ok(()) } - fn callback(&self, cb: ControllerCallback) { - if self.0.callback.send(Some(cb)).is_err() { + fn callback(&self, cb: impl Into>) { + if self.0.callback.send(Some(cb.into())).is_err() { // TODO should we panic? we failed what we were supposed to do tracing::error!("no active buffer worker to run registered callback!"); } diff --git a/src/cursor/controller.rs b/src/cursor/controller.rs index fe862e4..281d412 100644 --- a/src/cursor/controller.rs +++ b/src/cursor/controller.rs @@ -63,8 +63,8 @@ impl Controller for CursorController { Ok(self.0.last_op.lock().await.changed().await?) } - fn callback(&self, cb: ControllerCallback) { - if self.0.callback.send(Some(cb)).is_err() { + fn callback(&self, cb: impl Into>) { + if self.0.callback.send(Some(cb.into())).is_err() { // TODO should we panic? we failed what we were supposed to do tracing::error!("no active cursor worker to run registered callback!"); } diff --git a/src/ffi/lua.rs b/src/ffi/lua.rs index 026b625..723c340 100644 --- a/src/ffi/lua.rs +++ b/src/ffi/lua.rs @@ -207,12 +207,12 @@ impl LuaUserData for CodempBufferController { methods.add_method("clear_callback", |_, this, ()| Ok(this.clear_callback())); methods.add_method("callback", |_, this, (cb,):(LuaFunction,)| { - this.callback(ControllerCallback::from(move |controller: CodempBufferController| { + this.callback(move |controller: CodempBufferController| { let _c = controller.clone(); if let Err(e) = cb.call::<(CodempBufferController,), ()>((controller,)) { tracing::error!("error running buffer#{} callback: {e}", _c.name()); } - })); + }); Ok(()) }); }