async_callback_manager/
lib.rsuse futures::Future;
use futures::FutureExt;
use futures::Stream;
use std::any::Any;
use tokio::sync::oneshot;
mod adaptors;
mod error;
mod manager;
mod sender;
mod task;
pub use adaptors::*;
pub use error::*;
pub use manager::*;
pub use sender::*;
pub use task::Constraint;
pub trait BkendMap<Bkend> {
fn map(backend: &Bkend) -> &Self;
}
pub trait BackendTask<Bkend>: Send + Any {
type Output: Send;
type MetadataType: PartialEq;
fn into_future(self, backend: &Bkend) -> impl Future<Output = Self::Output> + Send + 'static;
fn metadata() -> Vec<Self::MetadataType> {
vec![]
}
}
pub trait BackendStreamingTask<Bkend>: Send + Any {
type Output: Send;
type MetadataType: PartialEq;
fn into_stream(
self,
backend: &Bkend,
) -> impl Stream<Item = Self::Output> + Send + Unpin + 'static;
fn metadata() -> Vec<Self::MetadataType> {
vec![]
}
}
struct KillHandle(Option<oneshot::Sender<()>>);
struct KillSignal(oneshot::Receiver<()>);
impl KillHandle {
fn kill(&mut self) -> Result<()> {
if let Some(tx) = self.0.take() {
return tx.send(()).map_err(|_| Error::ReceiverDropped);
}
Ok(())
}
}
impl Future for KillSignal {
type Output = Result<()>;
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.0.poll_unpin(cx).map_err(|_| Error::ReceiverDropped)
}
}
fn kill_channel() -> (KillHandle, KillSignal) {
let (tx, rx) = oneshot::channel();
(KillHandle(Some(tx)), KillSignal(rx))
}
type DynFallibleFuture = Box<dyn Future<Output = Result<()>> + Unpin + Send>;
type DynCallbackFn<Frntend> = Box<dyn FnOnce(&mut Frntend) + Send>;
type DynBackendTask<Bkend> = Box<dyn FnOnce(&Bkend) -> DynFallibleFuture>;