use futures::executor::ThreadPool;
use std::{future::Future, pin::Pin};
pub trait Executor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>);
}
impl<F: Fn(Pin<Box<dyn Future<Output = ()> + Send>>)> Executor for F {
fn exec(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
self(f)
}
}
impl Executor for ThreadPool {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
self.spawn_ok(future)
}
}
#[cfg(all(
feature = "tokio",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[derive(Default, Debug, Clone, Copy)]
pub struct TokioExecutor;
#[cfg(all(
feature = "tokio",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
impl Executor for TokioExecutor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
let _ = tokio::spawn(future);
}
}
#[cfg(all(
feature = "async-std",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[derive(Default, Debug, Clone, Copy)]
pub struct AsyncStdExecutor;
#[cfg(all(
feature = "async-std",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
impl Executor for AsyncStdExecutor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
let _ = async_std::task::spawn(future);
}
}
#[cfg(feature = "wasm-bindgen")]
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct WasmBindgenExecutor;
#[cfg(feature = "wasm-bindgen")]
impl Executor for WasmBindgenExecutor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
wasm_bindgen_futures::spawn_local(future)
}
}