feat: attempt callback impl on generic controller

This commit is contained in:
əlemi 2023-08-17 04:36:59 +02:00
parent 1cc03838eb
commit bbdcfe2712

View file

@ -22,6 +22,8 @@ pub mod proto {
pub use errors::Error; pub use errors::Error;
use std::sync::Arc;
#[tonic::async_trait] // TODO move this somewhere? #[tonic::async_trait] // TODO move this somewhere?
pub(crate) trait ControllerWorker<T> { pub(crate) trait ControllerWorker<T> {
type Controller : Controller<T>; type Controller : Controller<T>;
@ -33,9 +35,20 @@ pub(crate) trait ControllerWorker<T> {
} }
#[tonic::async_trait] #[tonic::async_trait]
pub trait Controller<T> { pub trait Controller<T> : Sized + Send + Sync {
type Input; type Input;
async fn send(&self, x: Self::Input) -> Result<(), Error>; async fn send(&self, x: Self::Input) -> Result<(), Error>;
async fn recv(&self) -> Result<T, Error>; async fn recv(&self) -> Result<T, Error>;
fn callback<F>(self: Arc<Self>, 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)
}
});
}
} }