use futures_task::{LocalSpawn, Spawn};
#[cfg(feature = "compat")]
use crate::compat::Compat;
#[cfg(feature = "channel")]
#[cfg(feature = "std")]
use crate::future::{FutureExt, RemoteHandle};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use futures_core::future::Future;
#[cfg(feature = "alloc")]
use futures_task::{FutureObj, LocalFutureObj, SpawnError};
impl<Sp: ?Sized> SpawnExt for Sp where Sp: Spawn {}
impl<Sp: ?Sized> LocalSpawnExt for Sp where Sp: LocalSpawn {}
pub trait SpawnExt: Spawn {
#[cfg(feature = "alloc")]
fn spawn<Fut>(&self, future: Fut) -> Result<(), SpawnError>
where
Fut: Future<Output = ()> + Send + 'static,
{
self.spawn_obj(FutureObj::new(Box::new(future)))
}
#[cfg(feature = "channel")]
#[cfg(feature = "std")]
fn spawn_with_handle<Fut>(&self, future: Fut) -> Result<RemoteHandle<Fut::Output>, SpawnError>
where
Fut: Future + Send + 'static,
Fut::Output: Send,
{
let (future, handle) = future.remote_handle();
self.spawn(future)?;
Ok(handle)
}
#[cfg(feature = "compat")]
fn compat(self) -> Compat<Self>
where
Self: Sized,
{
Compat::new(self)
}
}
pub trait LocalSpawnExt: LocalSpawn {
#[cfg(feature = "alloc")]
fn spawn_local<Fut>(&self, future: Fut) -> Result<(), SpawnError>
where
Fut: Future<Output = ()> + 'static,
{
self.spawn_local_obj(LocalFutureObj::new(Box::new(future)))
}
#[cfg(feature = "channel")]
#[cfg(feature = "std")]
fn spawn_local_with_handle<Fut>(
&self,
future: Fut,
) -> Result<RemoteHandle<Fut::Output>, SpawnError>
where
Fut: Future + 'static,
{
let (future, handle) = future.remote_handle();
self.spawn_local(future)?;
Ok(handle)
}
}