use core::future::Future;
pub trait AsyncSpawner: Copy + Send + Sync + 'static {
type JoinHandle<F>: Future + Send + Sync + 'static
where
F: Send + 'static;
fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
where
F::Output: Send + 'static,
F: Future + Send + 'static;
fn spawn_detach<F>(future: F)
where
F::Output: Send + 'static,
F: Future + Send + 'static,
{
core::mem::drop(Self::spawn(future));
}
}
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
#[derive(Debug, Clone, Copy)]
pub struct TokioSpawner;
#[cfg(feature = "tokio")]
impl AsyncSpawner for TokioSpawner {
type JoinHandle<F> = tokio::task::JoinHandle<F> where
F: Send + 'static;
fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
{
tokio::task::spawn(future)
}
}
#[cfg(feature = "async-std")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
#[derive(Debug, Clone, Copy)]
pub struct AsyncStdSpawner;
#[cfg(feature = "async-std")]
impl AsyncSpawner for AsyncStdSpawner {
type JoinHandle<F> = async_std::task::JoinHandle<F> where F: Send + 'static;
fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
{
async_std::task::spawn(future)
}
}
#[cfg(feature = "smol")]
#[cfg_attr(docsrs, doc(cfg(feature = "smol")))]
#[derive(Debug, Clone, Copy)]
pub struct SmolSpawner;
#[cfg(feature = "smol")]
impl AsyncSpawner for SmolSpawner {
type JoinHandle<F> = smol::Task<F> where F: Send + 'static;
fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
{
smol::spawn(future)
}
fn spawn_detach<F>(future: F)
where
F::Output: Send + 'static,
F: Future + Send + 'static,
{
smol::spawn(future).detach()
}
}