@@ -8,18 +8,18 @@ use futures::{
8
8
use itertools:: Itertools ;
9
9
use tokio:: task:: JoinSet ;
10
10
11
- pub struct TaskManager < T > {
11
+ pub struct TaskManager < T , N > {
12
12
set : JoinSet < anyhow:: Result < T > > ,
13
- cancel_token : CancellationToken ,
13
+ cancel_token : CancellationToken < N > ,
14
14
}
15
15
16
- #[ async_trait:: async_trait]
17
16
pub trait NotifyCancel {
18
- async fn wait_until_cancelled ( & self ) -> anyhow:: Result < ( ) > ;
17
+ fn wait_until_cancelled (
18
+ & self ,
19
+ ) -> impl core:: future:: Future < Output = anyhow:: Result < ( ) > > + Send ;
19
20
fn is_cancelled ( & self ) -> bool ;
20
21
}
21
22
22
- #[ async_trait:: async_trait]
23
23
impl NotifyCancel for tokio_util:: sync:: CancellationToken {
24
24
async fn wait_until_cancelled ( & self ) -> anyhow:: Result < ( ) > {
25
25
self . cancelled ( ) . await ;
@@ -30,7 +30,6 @@ impl NotifyCancel for tokio_util::sync::CancellationToken {
30
30
}
31
31
}
32
32
33
- #[ async_trait:: async_trait]
34
33
impl NotifyCancel for StateWatcher {
35
34
async fn wait_until_cancelled ( & self ) -> anyhow:: Result < ( ) > {
36
35
let mut state = self . clone ( ) ;
@@ -50,13 +49,16 @@ impl NotifyCancel for StateWatcher {
50
49
/// A token that implements [`NotifyCancel`]. Given to jobs inside of [`TaskManager`] so they can
51
50
/// stop either when commanded by the [`TaskManager`] or by an outside source.
52
51
#[ derive( Clone ) ]
53
- pub struct CancellationToken {
54
- outside_signal : Arc < dyn NotifyCancel + Send + Sync > ,
52
+ pub struct CancellationToken < N > {
53
+ outside_signal : Arc < N > ,
55
54
inner_signal : tokio_util:: sync:: CancellationToken ,
56
55
}
57
56
58
- impl CancellationToken {
59
- pub fn new ( outside_signal : impl NotifyCancel + Send + Sync + ' static ) -> Self {
57
+ impl < N > CancellationToken < N >
58
+ where
59
+ N : NotifyCancel ,
60
+ {
61
+ pub fn new ( outside_signal : N ) -> Self {
60
62
Self {
61
63
outside_signal : Arc :: new ( outside_signal) ,
62
64
inner_signal : tokio_util:: sync:: CancellationToken :: new ( ) ,
@@ -66,16 +68,17 @@ impl CancellationToken {
66
68
pub fn cancel ( & self ) {
67
69
self . inner_signal . cancel ( )
68
70
}
69
- }
70
71
71
- impl CancellationToken {
72
72
pub fn is_cancelled ( & self ) -> bool {
73
73
self . inner_signal . is_cancelled ( ) || self . outside_signal . is_cancelled ( )
74
74
}
75
75
}
76
76
77
- impl < T > TaskManager < T > {
78
- pub fn new ( outside_cancel : impl NotifyCancel + Send + Sync + ' static ) -> Self {
77
+ impl < T , N > TaskManager < T , N >
78
+ where
79
+ N : NotifyCancel + Clone ,
80
+ {
81
+ pub fn new ( outside_cancel : N ) -> Self {
79
82
Self {
80
83
set : JoinSet :: new ( ) ,
81
84
cancel_token : CancellationToken :: new ( outside_cancel) ,
@@ -84,20 +87,21 @@ impl<T> TaskManager<T> {
84
87
85
88
pub fn run < F > ( & mut self , arg : F ) -> anyhow:: Result < T >
86
89
where
87
- F : FnOnce ( CancellationToken ) -> anyhow:: Result < T > ,
90
+ F : FnOnce ( CancellationToken < N > ) -> anyhow:: Result < T > ,
88
91
{
89
92
arg ( self . cancel_token . clone ( ) )
90
93
}
91
94
}
92
95
93
- impl < T > TaskManager < T >
96
+ impl < T , N > TaskManager < T , N >
94
97
where
95
98
T : Send + ' static ,
99
+ N : NotifyCancel + Send + Sync + Clone + ' static ,
96
100
{
97
101
#[ cfg( test) ]
98
102
pub fn spawn < F , Fut > ( & mut self , arg : F )
99
103
where
100
- F : FnOnce ( CancellationToken ) -> Fut ,
104
+ F : FnOnce ( CancellationToken < N > ) -> Fut ,
101
105
Fut : futures:: Future < Output = anyhow:: Result < T > > + Send + ' static ,
102
106
{
103
107
let token = self . cancel_token . clone ( ) ;
@@ -106,7 +110,7 @@ where
106
110
107
111
pub fn spawn_blocking < F > ( & mut self , arg : F )
108
112
where
109
- F : FnOnce ( CancellationToken ) -> anyhow:: Result < T > + Send + ' static ,
113
+ F : FnOnce ( CancellationToken < N > ) -> anyhow:: Result < T > + Send + ' static ,
110
114
{
111
115
let token = self . cancel_token . clone ( ) ;
112
116
self . set . spawn_blocking ( move || arg ( token) ) ;
0 commit comments