async_executors/iface/
local_spawn_handle.rs

1#[ allow(unused_imports) ]
2//
3use
4{
5	futures_task :: { SpawnError, LocalFutureObj                                          } ,
6	futures_util :: { task::{ LocalSpawnExt }, future::{ FutureExt, abortable }           } ,
7	crate        :: { JoinHandle                                                          } ,
8	std          :: { pin::Pin, future::Future, sync::{ Arc, atomic::AtomicBool }, rc::Rc } ,
9	blanket      :: { blanket                                                             } ,
10};
11
12
13/// This is similar to [`SpawnHandle`](crate::SpawnHandle) except that it allows spawning `!Send` futures. Please see
14/// the docs on [`SpawnHandle`](crate::SpawnHandle).
15//
16#[ blanket(derive( Ref, Mut, Box, Arc, Rc )) ]
17//
18pub trait LocalSpawnHandle<Out: 'static>
19{
20	/// Spawn a future and return a [`JoinHandle`] that can be awaited for the output of the future.
21	//
22	fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>;
23}
24
25
26/// Lets you spawn a `!Send` future and get a [`JoinHandle`] to await the output of a future.
27//
28pub trait LocalSpawnHandleExt<Out: 'static> : LocalSpawnHandle<Out>
29{
30	/// Convenience trait for passing in a generic future to [`LocalSpawnHandle`]. Much akin to `LocalSpawn` and `LocalSpawnExt` in the
31	/// futures library.
32	//
33	fn spawn_handle_local( &self, future: impl Future<Output = Out> + 'static ) -> Result<JoinHandle<Out>, SpawnError>;
34}
35
36
37impl<T, Out> LocalSpawnHandleExt<Out> for T
38
39	where T  : LocalSpawnHandle<Out> + ?Sized ,
40	      Out: 'static                        ,
41{
42	fn spawn_handle_local( &self, future: impl Future<Output = Out> + 'static ) -> Result<JoinHandle<Out>, SpawnError>
43	{
44		self.spawn_handle_local_obj( LocalFutureObj::new(future.boxed_local()) )
45	}
46}