use std::{fmt::Debug, future::Future, pin::Pin};
pub trait Executor: Clone + Sync + Send + 'static {
fn spawn<F>(&self, future: F)
where
F: Future + Send + 'static,
F::Output: Send;
fn spawn_blocking<F, R>(&self, func: F)
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static;
}
pub trait Timer: Send + Sync + 'static {
fn sleep(&self, duration: std::time::Duration) -> Pin<Box<dyn EventFuture>>;
}
pub trait EventFuture: Send + Future<Output = ()> {}
impl<T> EventFuture for T where T: Future<Output = ()> + Send {}
pub trait CancelToken: Send + Sync + 'static {
fn wait(&self) -> Pin<Box<dyn EventFuture>>;
fn is_cancelled(&self) -> bool;
fn cancel(&self);
fn on_cancel(&self, callback: Box<dyn FnOnce() + Send + Sync>);
fn clone_box(&self) -> Box<dyn CancelToken>;
}
pub type BoxedCancelToken = Box<dyn CancelToken>;
impl Clone for BoxedCancelToken {
fn clone(&self) -> Self {
self.clone_box()
}
}
impl Debug for dyn CancelToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CancelToken")
.field("cancelled", &self.is_cancelled())
.finish()
}
}