1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#[ allow(unused_imports) ]
//
use
{
	futures_util :: { future::{ FutureExt, abortable }, task::SpawnExt                    } ,
	futures_task :: { SpawnError, FutureObj                                               } ,
	crate        :: { BlockingHandle                                                      } ,
	std          :: { pin::Pin, future::Future, sync::{ Arc, atomic::AtomicBool }, rc::Rc } ,
	blanket      :: { blanket                                                             } ,
};


/// Indicate the executor can provide a threadpool for blocking operations.
/// There is two methods of this trait. One of them requires boxing the closure
/// and the other is not object safe.
//
// Doesn't work with blanket.
// #[ blanket(derive( Mut, Box, Arc, Rc )) ]
//
pub trait SpawnBlocking<R> where R: Send + 'static
{
	/// Runs the provided closure on a thread where blocking is acceptable.
	//
	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>

		where F   : FnOnce() -> R + Send + 'static ,
	         Self: Sized                          ,
	;

	/// Runs the provided closure on a thread where blocking is acceptable. This part of the trait is
	/// object safe but your closure must be boxed and you cannot have a return value.
	//
	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>;
}


impl<R, T> SpawnBlocking<R> for &T  where T: SpawnBlocking<R>, R: Send + 'static
{
	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>

		where F: FnOnce() -> R + Send + 'static ,
	         T: Sized                          ,
	{
		(**self).spawn_blocking( f )
	}


	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
	{
		(**self).spawn_blocking_dyn( f )
	}
}


impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for &mut T where T: SpawnBlocking<R>, R: Send + 'static
{
	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>

		where F: FnOnce() -> R + Send + 'static ,
	         T: Sized                          ,
	{
		(**self).spawn_blocking( f )
	}


	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
	{
		(**self).spawn_blocking_dyn( f )
	}
}


impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for Box<T> where T: SpawnBlocking<R>, R: Send + 'static
{
	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>

		where F: FnOnce() -> R + Send + 'static ,
	         T: Sized                          ,
	{
		(**self).spawn_blocking( f )
	}


	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
	{
		(**self).spawn_blocking_dyn( f )
	}
}


impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for Arc<T> where T: SpawnBlocking<R>, R: Send + 'static
{
	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>

		where F: FnOnce() -> R + Send + 'static ,
	         T: Sized                          ,
	{
		(**self).spawn_blocking( f )
	}


	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
	{
		(**self).spawn_blocking_dyn( f )
	}
}


impl<R, T: SpawnBlocking<R>> SpawnBlocking<R> for Rc<T> where T: SpawnBlocking<R>, R: Send + 'static
{
	fn spawn_blocking<F>( &self, f: F ) -> BlockingHandle<R>

		where F: FnOnce() -> R + Send + 'static ,
	         T: Sized                          ,
	{
		(**self).spawn_blocking( f )
	}


	fn spawn_blocking_dyn( &self, f: Box< dyn FnOnce()->R + Send > ) -> BlockingHandle<R>
	{
		(**self).spawn_blocking_dyn( f )
	}
}