From bbdcfe2712d8e53d42c99f7bd7f113e9d9b48631 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 17 Aug 2023 04:36:59 +0200 Subject: [PATCH] feat: attempt callback impl on generic controller --- src/lib.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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) + } + }); + } }