diff --git a/src/lib.rs b/src/lib.rs index 9c835a3..7a3e843 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,8 @@ pub mod proto { pub use errors::Error; +use std::sync::Arc; + #[tonic::async_trait] // TODO move this somewhere? pub(crate) trait ControllerWorker { type Controller : Controller; @@ -33,9 +35,20 @@ pub(crate) trait ControllerWorker { } #[tonic::async_trait] -pub trait Controller { +pub trait Controller : Sized + Send + Sync { type Input; async fn send(&self, x: Self::Input) -> Result<(), Error>; async fn recv(&self) -> Result; + + fn callback(self: Arc, mut cb: F) + where Self : 'static, F : FnMut(T) + Sync + Send + 'static + { + let x = Arc::new(self); + tokio::spawn(async move { + while let Ok(data) = x.recv().await { + cb(data) + } + }); + } }