@@ -7,11 +7,9 @@ use std::{
7
7
use async_executor:: { Executor , Task } ;
8
8
use futures_lite:: Future ;
9
9
10
- /// Use to access the global main thread executor. Be aware that the main thread executor
11
- /// only makes progress when it is ticked. This normally happens in `[TaskPool::scope]`.
10
+ /// An executor that can only be ticked on the thread it was instantiated on.
12
11
#[ derive( Debug ) ]
13
12
pub struct ThreadExecutor {
14
- // this is only pub crate for testing purposes, do not contruct otherwise
15
13
executor : Arc < Executor < ' static > > ,
16
14
thread_id : ThreadId ,
17
15
}
@@ -26,13 +24,13 @@ impl Default for ThreadExecutor {
26
24
}
27
25
28
26
impl ThreadExecutor {
29
- /// Initializes the global `[MainThreadExecutor]` instance.
27
+ /// createa a new `[ThreadExecutor]`
30
28
pub fn new ( ) -> Self {
31
29
Self :: default ( )
32
30
}
33
31
34
- /// Gets the `[MainThreadSpawner]` for the global main thread executor.
35
- /// Use this to spawn tasks on the main thread.
32
+ /// Gets the `[MainThreadSpawner]` for the thread executor.
33
+ /// Use this to spawn tasks that run on the thread this was instatiated on .
36
34
pub fn spawner ( & self ) -> MainThreadSpawner < ' static > {
37
35
MainThreadSpawner ( self . executor . clone ( ) )
38
36
}
@@ -69,8 +67,29 @@ pub struct MainThreadTicker {
69
67
}
70
68
impl MainThreadTicker {
71
69
/// Tick the main thread executor.
72
- /// This needs to be called manually on the main thread if a `[TaskPool::scope]` is not active
70
+ /// This needs to be called manually on the thread if it is not being used with
71
+ /// a `[TaskPool::scope]`.
73
72
pub fn tick ( & self ) -> impl Future < Output = ( ) > + ' _ {
74
73
self . executor . tick ( )
75
74
}
76
75
}
76
+
77
+ #[ cfg( test) ]
78
+ mod tests {
79
+ use super :: * ;
80
+ use std:: sync:: Arc ;
81
+
82
+ #[ test]
83
+ fn test_ticker ( ) {
84
+ let executor = Arc :: new ( ThreadExecutor :: new ( ) ) ;
85
+ let ticker = executor. ticker ( ) ;
86
+ assert ! ( ticker. is_some( ) ) ;
87
+
88
+ std:: thread:: scope ( |s| {
89
+ s. spawn ( || {
90
+ let ticker = executor. ticker ( ) ;
91
+ assert ! ( ticker. is_none( ) ) ;
92
+ } ) ;
93
+ } ) ;
94
+ }
95
+ }
0 commit comments