async_executors/iface/
spawn_blocking.rs

1#[ allow(unused_imports) ]
2//
3use
4{
5	futures_util :: { future::{ FutureExt, abortable }, task::SpawnExt                    } ,
6	futures_task :: { SpawnError, FutureObj                                               } ,
7	crate        :: { BlockingHandle                                                      } ,
8	std          :: { pin::Pin, future::Future, sync::{ Arc, atomic::AtomicBool }, rc::Rc } ,
9	blanket      :: { blanket                                                             } ,
10};
11
12
13/// Indicate the executor can provide a threadpool for blocking operations.
14/// There is two methods of this trait. One of them requires boxing the closure
15/// and the other is not object safe.
16//
17// Doesn't work with blanket.
18// #[ blanket(derive( Mut, Box, Arc, Rc )) ]
19//
20pub trait SpawnBlocking<R> where R: Send + 'static
21{
22	/// Runs the provided closure on a thread where blocking is acceptable.
23	//
24	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>
25
26		where F   : FnOnce() -> R + Send + 'static ,
27	         Self: Sized                          ,
28	;
29
30	/// Runs the provided closure on a thread where blocking is acceptable. This part of the trait is
31	/// object safe but your closure must be boxed and you cannot have a return value.
32	//
33	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>;
34}
35
36
37impl<R, T> SpawnBlocking<R> for &T  where T: SpawnBlocking<R>, R: Send + 'static
38{
39	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>
40
41		where F: FnOnce() -> R + Send + 'static ,
42	         T: Sized                          ,
43	{
44		(**self).spawn_blocking( f )
45	}
46
47
48	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
49	{
50		(**self).spawn_blocking_dyn( f )
51	}
52}
53
54
55impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for &mut T where T: SpawnBlocking<R>, R: Send + 'static
56{
57	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>
58
59		where F: FnOnce() -> R + Send + 'static ,
60	         T: Sized                          ,
61	{
62		(**self).spawn_blocking( f )
63	}
64
65
66	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
67	{
68		(**self).spawn_blocking_dyn( f )
69	}
70}
71
72
73impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for Box<T> where T: SpawnBlocking<R>, R: Send + 'static
74{
75	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>
76
77		where F: FnOnce() -> R + Send + 'static ,
78	         T: Sized                          ,
79	{
80		(**self).spawn_blocking( f )
81	}
82
83
84	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
85	{
86		(**self).spawn_blocking_dyn( f )
87	}
88}
89
90
91impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for Arc<T> where T: SpawnBlocking<R>, R: Send + 'static
92{
93	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>
94
95		where F: FnOnce() -> R + Send + 'static ,
96	         T: Sized                          ,
97	{
98		(**self).spawn_blocking( f )
99	}
100
101
102	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
103	{
104		(**self).spawn_blocking_dyn( f )
105	}
106}
107
108
109impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for Rc<T> where T: SpawnBlocking<R>, R: Send + 'static
110{
111	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>
112
113		where F: FnOnce() -> R + Send + 'static ,
114	         T: Sized                          ,
115	{
116		(**self).spawn_blocking( f )
117	}
118
119
120	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
121	{
122		(**self).spawn_blocking_dyn( f )
123	}
124}