mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-21 23:04:49 +01:00
feat: attempt callback impl on generic controller
This commit is contained in:
parent
1cc03838eb
commit
bbdcfe2712
1 changed files with 14 additions and 1 deletions
15
src/lib.rs
15
src/lib.rs
|
@ -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)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue