From d3ce714070359e25b6e77f1e6fe97f90523955db Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 15 Aug 2024 20:15:23 +0200 Subject: [PATCH] feat: rather than +Debug, make a newtype --- src/api/controller.rs | 24 +++++++++++++++++++----- src/buffer/worker.rs | 2 +- src/cursor/worker.rs | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/api/controller.rs b/src/api/controller.rs index 1e0e736..15dbdb7 100644 --- a/src/api/controller.rs +++ b/src/api/controller.rs @@ -70,9 +70,23 @@ pub trait Controller : Sized + Send + Sync { } -/// type alias for Boxed dyn callback -pub type ControllerCallback = Box; +/// type wrapper for Boxed dyn callback +pub struct ControllerCallback(Box); -/// underlying trait for controller callback: must be a threadsafe repeatable non-mut closure which -/// can be debug printed -pub trait ControllerCallbackTrait : Sync + Send + std::fmt::Debug + Fn() {} +impl ControllerCallback { + pub fn call(&self) { + self.0() // lmao at this syntax + } +} + +impl std::fmt::Debug for ControllerCallback { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ControllerCallback {{ {:p} }}", self.0) + } +} + +impl From for ControllerCallback { + fn from(value: T) -> Self { + Self(Box::new(value)) + } +} diff --git a/src/buffer/worker.rs b/src/buffer/worker.rs index d59a207..3d416ed 100644 --- a/src/buffer/worker.rs +++ b/src/buffer/worker.rs @@ -135,7 +135,7 @@ impl ControllerWorker for BufferWorker { tx.send(()).unwrap_or_warn("could not wake up poller"); } if let Some(cb) = self.callback.borrow().as_ref() { - cb(); // TODO should we run this on another task/thread? + cb.call(); // TODO should we run this on another task/thread? } }, Err(e) => tracing::error!("could not deserialize operation from server: {}", e), diff --git a/src/cursor/worker.rs b/src/cursor/worker.rs index 4dddd6e..1d92beb 100644 --- a/src/cursor/worker.rs +++ b/src/cursor/worker.rs @@ -62,7 +62,7 @@ impl ControllerWorker for CursorWorker { self.channel.send(cur.clone()).unwrap_or_warn("could not broadcast event"); self.changed.send(cur).unwrap_or_warn("could not update last event"); if let Some(cb) = self.callback.borrow().as_ref() { - cb(); // TODO should this run in its own task/thread? + cb.call(); // TODO should this run in its own task/thread? } }, else => break,