codemp/src/lib.rs

55 lines
1 KiB
Rust
Raw Normal View History

pub mod cursor;
pub mod errors;
pub mod buffer;
pub mod client;
#[cfg(feature = "static")]
pub mod instance;
pub mod prelude;
pub use tonic;
pub use tokio;
pub use operational_transform as ot;
#[cfg(feature = "proto")]
2023-08-16 17:08:31 +02:00
#[allow(non_snake_case)]
pub mod proto {
2023-08-16 17:08:31 +02:00
tonic::include_proto!("codemp.buffer");
tonic::include_proto!("codemp.cursor");
}
pub use errors::Error;
use std::sync::Arc;
#[tonic::async_trait] // TODO move this somewhere?
pub(crate) trait ControllerWorker<T> {
type Controller : Controller<T>;
type Tx;
type Rx;
fn subscribe(&self) -> Self::Controller;
async fn work(self, tx: Self::Tx, rx: Self::Rx);
}
#[tonic::async_trait]
pub trait Controller<T> : Sized + Send + Sync {
type Input;
async fn send(&self, x: Self::Input) -> Result<(), 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)
}
});
}
}