use
{
crate :: { SpawnHandle, LocalSpawnHandle, JoinHandle, join_handle::InnerJh } ,
futures_task :: { FutureObj, LocalFutureObj, Spawn, LocalSpawn, SpawnError } ,
futures_util :: { future::abortable } ,
std :: { sync::atomic::AtomicBool } ,
};
#[ derive( Copy, Clone, Default ) ]
#[ cfg_attr( nightly, doc(cfg( feature = "async_std" )) ) ]
pub struct AsyncStd;
impl AsyncStd
{
pub fn new() -> Self
{
Self::default()
}
#[cfg(not(target_os = "unknown"))]
#[ cfg_attr( nightly, doc(cfg(not( target_os = "unknown" ))) ) ]
pub fn block_on<F: std::future::Future>(future: F) -> F::Output
{
async_std_crate::task::block_on( future )
}
}
#[ cfg( target_arch = "wasm32" ) ]
impl Spawn for AsyncStd
{
fn spawn_obj( &self, future: FutureObj<'static, ()> ) -> Result<(), SpawnError>
{
async_std_crate::task::spawn_local( future );
Ok(())
}
}
#[ cfg(not( target_arch = "wasm32" )) ]
impl Spawn for AsyncStd
{
fn spawn_obj( &self, future: FutureObj<'static, ()> ) -> Result<(), SpawnError>
{
async_std_crate::task::spawn( future );
Ok(())
}
}
#[ cfg( not(target_arch = "wasm32") ) ]
impl<Out: 'static + Send> SpawnHandle<Out> for AsyncStd
{
fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (fut, a_handle) = abortable( future );
Ok( JoinHandle{ inner: crate::join_handle::InnerJh::AsyncStd
{
handle : async_std_crate::task::spawn( fut ) ,
detached: AtomicBool::new( false ) ,
a_handle ,
}})
}
}
#[ cfg( target_arch = "wasm32" ) ]
impl<Out: 'static + Send> SpawnHandle<Out> for AsyncStd
{
fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (fut, a_handle) = abortable( future );
Ok( JoinHandle{ inner: InnerJh::AsyncStd
{
handle : async_std_crate::task::spawn_local( fut ) ,
detached: AtomicBool::new( false ) ,
a_handle ,
}})
}
}
impl<Out: 'static> LocalSpawnHandle<Out> for AsyncStd
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (fut, a_handle) = abortable( future );
Ok( JoinHandle{ inner: InnerJh::AsyncStd
{
handle : async_std_crate::task::spawn_local( fut ) ,
detached: AtomicBool::new( false ) ,
a_handle ,
}})
}
}
impl LocalSpawn for AsyncStd
{
fn spawn_local_obj( &self, future: LocalFutureObj<'static, ()> ) -> Result<(), SpawnError>
{
let _ = async_std_crate::task::spawn_local( future );
Ok(())
}
}
impl std::fmt::Debug for AsyncStd
{
fn fmt( &self, f: &mut std::fmt::Formatter<'_> ) -> std::fmt::Result
{
write!( f, "AsyncStd executor" )
}
}