use core::future::Future;
pub trait Spawner {
fn spawn<F>(&self, task: F)
where
F: Future<Output = ()> + Send + 'static;
}
pub trait LocalSpawner {
fn spawn_local<F>(&self, task: F)
where
F: Future<Output = ()> + 'static;
}
#[cfg(feature = "tokio")]
#[derive(Debug, Clone, Copy, Default)]
pub struct Tokio;
#[cfg(feature = "tokio")]
impl Spawner for Tokio {
fn spawn<F>(&self, task: F)
where
F: Future<Output = ()> + Send + 'static,
{
tokio::spawn(task);
}
}
#[cfg(feature = "tokio")]
impl Spawner for tokio::runtime::Handle {
fn spawn<F>(&self, task: F)
where
F: Future<Output = ()> + Send + 'static,
{
tokio::runtime::Handle::spawn(self, task);
}
}
#[cfg(feature = "smol")]
#[derive(Debug, Clone, Copy, Default)]
pub struct Smol;
#[cfg(feature = "smol")]
impl Spawner for Smol {
fn spawn<F>(&self, task: F)
where
F: Future<Output = ()> + Send + 'static,
{
smol::spawn(task).detach();
}
}
#[cfg(feature = "smol")]
impl Spawner for smol::Executor<'static> {
fn spawn<F>(&self, task: F)
where
F: Future<Output = ()> + Send + 'static,
{
smol::Executor::spawn(self, task).detach();
}
}