Skip to content

Commit

Permalink
feat: rather than +Debug, make a newtype
Browse files Browse the repository at this point in the history
  • Loading branch information
alemidev committed Aug 15, 2024
1 parent 7900ca0 commit d3ce714
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
24 changes: 19 additions & 5 deletions src/api/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,23 @@ pub trait Controller<T : Sized + Send + Sync> : Sized + Send + Sync {
}


/// type alias for Boxed dyn callback
pub type ControllerCallback = Box<dyn ControllerCallbackTrait>;
/// type wrapper for Boxed dyn callback
pub struct ControllerCallback(Box<dyn Sync + Send + Fn()>);

/// 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<T: Sync + Send + Fn() + 'static> From<T> for ControllerCallback {
fn from(value: T) -> Self {
Self(Box::new(value))
}
}
2 changes: 1 addition & 1 deletion src/buffer/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl ControllerWorker<TextChange> 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),
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl ControllerWorker<Cursor> 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,
Expand Down

0 comments on commit d3ce714

Please sign in to comment.