feat: rather than +Debug, make a newtype

This commit is contained in:
əlemi 2024-08-15 20:15:23 +02:00
parent 7900ca08a7
commit d3ce714070
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 21 additions and 7 deletions

View file

@ -70,9 +70,23 @@ pub trait Controller<T : Sized + Send + Sync> : Sized + Send + Sync {
} }
/// type alias for Boxed dyn callback /// type wrapper for Boxed dyn callback
pub type ControllerCallback = Box<dyn ControllerCallbackTrait>; pub struct ControllerCallback(Box<dyn Sync + Send + Fn()>);
/// underlying trait for controller callback: must be a threadsafe repeatable non-mut closure which impl ControllerCallback {
/// can be debug printed pub fn call(&self) {
pub trait ControllerCallbackTrait : Sync + Send + std::fmt::Debug + Fn() {} 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))
}
}

View file

@ -135,7 +135,7 @@ impl ControllerWorker<TextChange> for BufferWorker {
tx.send(()).unwrap_or_warn("could not wake up poller"); tx.send(()).unwrap_or_warn("could not wake up poller");
} }
if let Some(cb) = self.callback.borrow().as_ref() { 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), Err(e) => tracing::error!("could not deserialize operation from server: {}", e),

View file

@ -62,7 +62,7 @@ impl ControllerWorker<Cursor> for CursorWorker {
self.channel.send(cur.clone()).unwrap_or_warn("could not broadcast event"); self.channel.send(cur.clone()).unwrap_or_warn("could not broadcast event");
self.changed.send(cur).unwrap_or_warn("could not update last event"); self.changed.send(cur).unwrap_or_warn("could not update last event");
if let Some(cb) = self.callback.borrow().as_ref() { 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, else => break,