use futures::FutureExt;
use crate::invocation::ContextOf;
use crate::invocation::StateOf;
use crate::Invocation;
pub type ErrorOf<E, I, V, B> = <E as Executor<I, V, B>>::Error;
pub trait Executor<I: Invocation, V, B>: 'static {
type Error;
fn execute<F: std::future::Future<Output = ()> + 'static>(
self,
task: crate::node::Task<I, V, B, F>,
) -> Result<(), Self::Error>;
}
impl<I, V, B, E> Executor<I, V, B> for E
where
I: Invocation + Send + 'static,
StateOf<I>: Send,
ContextOf<I>: Send,
V: Send + 'static,
B: Send + 'static,
E: futures::task::Spawn + 'static,
{
type Error = futures::task::SpawnError;
fn execute<F: std::future::Future<Output = ()> + 'static>(
self,
task: crate::node::Task<I, V, B, F>,
) -> Result<(), Self::Error> {
self.spawn_obj(futures::task::FutureObj::from(task.boxed()))
}
}
pub struct StdThread;
impl Default for StdThread {
fn default() -> Self {
Self
}
}
impl<I, V, B> Executor<I, V, B> for StdThread
where
I: Invocation + Send + 'static,
StateOf<I>: Send,
ContextOf<I>: Send,
V: Send + 'static,
B: Send + 'static,
{
type Error = std::io::Error;
fn execute<F: std::future::Future<Output = ()> + 'static>(
self,
task: crate::node::Task<I, V, B, F>,
) -> Result<(), Self::Error> {
let thread_buidler = std::thread::Builder::new();
thread_buidler
.spawn(|| {
futures::executor::block_on(task);
})
.map(|_| ())
}
}
#[cfg(feature = "wasm-bindgen-futures")]
pub struct WasmExecutor;
#[cfg(feature = "wasm-bindgen-futures")]
impl<I, V, B> Executor<I, V, B> for WasmExecutor
where
I: Invocation + 'static,
V: 'static,
B: 'static,
{
type Error = ();
fn execute<F: std::future::Future<Output = ()> + 'static>(
self,
task: crate::node::Task<I, V, B, F>,
) -> Result<(), Self::Error> {
wasm_bindgen_futures::spawn_local(task);
Ok(())
}
}