#[ allow(unused_imports) ]
use
{
futures_util :: { task::{ LocalSpawnExt, SpawnError, LocalFutureObj }, future::{ FutureExt, abortable } } ,
crate :: { import::*, JoinHandle, remote_handle::remote_handle } ,
std :: { pin::Pin, future::Future, sync::{ Arc, atomic::AtomicBool }, rc::Rc } ,
};
#[ cfg_attr( nightly, doc(cfg( feature = "spawn_handle" )) ) ]
pub trait LocalSpawnHandle<Out: 'static>
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>;
}
#[ cfg_attr( nightly, doc(cfg( feature = "spawn_handle" )) ) ]
pub trait LocalSpawnHandleExt<Out: 'static> : LocalSpawnHandle<Out>
{
fn spawn_handle_local( &self, future: impl Future<Output = Out> + 'static ) -> Result<JoinHandle<Out>, SpawnError>;
}
impl<T, Out> LocalSpawnHandleExt<Out> for T
where T : LocalSpawnHandle<Out> + ?Sized ,
Out: 'static ,
{
fn spawn_handle_local( &self, future: impl Future<Output = Out> + 'static ) -> Result<JoinHandle<Out>, SpawnError>
{
self.spawn_handle_local_obj( LocalFutureObj::new(future.boxed_local()) )
}
}
impl<T: ?Sized, Out> LocalSpawnHandle<Out> for Box<T> where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
(**self).spawn_handle_local_obj( future )
}
}
impl<T: ?Sized, Out> LocalSpawnHandle<Out> for Arc<T> where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
(**self).spawn_handle_local_obj( future )
}
}
impl<T: ?Sized, Out> LocalSpawnHandle<Out> for Rc<T> where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
(**self).spawn_handle_local_obj( future )
}
}
impl<T, Out> LocalSpawnHandle<Out> for &T where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
(**self).spawn_handle_local_obj( future )
}
}
impl<T, Out> LocalSpawnHandle<Out> for &mut T where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
(**self).spawn_handle_local_obj( future )
}
}
#[ cfg( feature = "tracing" ) ]
impl<T, Out> LocalSpawnHandle<Out> for Instrumented<T> where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let fut = future.instrument( self.span().clone() );
self.inner().spawn_handle_local_obj( LocalFutureObj::new(fut.boxed_local()) )
}
}
#[ cfg( feature = "tracing" ) ]
impl<T, Out> LocalSpawnHandle<Out> for WithDispatch<T> where T: LocalSpawnHandle<Out>, Out: 'static
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let fut = self.with_dispatch(future);
self.inner().spawn_handle_local_obj( LocalFutureObj::new(fut.boxed_local()) )
}
}
#[ cfg(any( feature = "tokio_ct" )) ]
impl<Out: 'static> LocalSpawnHandle<Out> for crate::TokioCt
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (fut, a_handle) = abortable( future );
Ok( JoinHandle{ inner: crate::join_handle::InnerJh::Tokio
{
handle : self.local.spawn_local( fut ) ,
detached: AtomicBool::new( false ) ,
a_handle ,
}})
}
}
#[ cfg( feature = "bindgen" ) ]
impl<Out: 'static> LocalSpawnHandle<Out> for crate::Bindgen
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (fut, handle) = remote_handle( future );
wasm_bindgen_futures::spawn_local(fut);
Ok( JoinHandle{ inner: crate::join_handle::InnerJh::RemoteHandle( Some(handle) ) } )
}
}
#[ cfg( feature = "localpool" ) ]
impl<Out: 'static> LocalSpawnHandle<Out> for futures_executor::LocalSpawner
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (fut, handle) = remote_handle( future );
self.spawn_local( fut )?;
Ok( JoinHandle{ inner: crate::join_handle::InnerJh::RemoteHandle( Some(handle) ) } )
}
}