1
+ //! Future and Stream cancellation
2
+
1
3
use std:: pin:: Pin ;
2
4
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
3
5
@@ -9,20 +11,25 @@ use async_std::task::{Context, Poll};
9
11
use event_listener:: { Event , EventListener } ;
10
12
use pin_project_lite:: pin_project;
11
13
14
+ /// StopSource produces [StopToken] and cancels all of its tokens on drop.
12
15
#[ derive( Debug ) ]
13
16
pub struct StopSource {
14
17
stopped : Arc < AtomicBool > ,
15
18
event : Arc < Event > ,
16
19
}
17
20
18
21
impl StopSource {
22
+ /// Create a new StopSource
19
23
pub fn new ( ) -> Self {
20
24
Self {
21
25
stopped : Arc :: new ( AtomicBool :: new ( false ) ) ,
22
26
event : Arc :: new ( Event :: new ( ) ) ,
23
27
}
24
28
}
25
29
30
+ /// Produce a new [StopToken], associated with this source.
31
+ ///
32
+ /// Once this source is dropped, all associated [StopToken] futures will complete.
26
33
pub fn token ( & self ) -> StopToken {
27
34
StopToken {
28
35
stopped : self . stopped . clone ( ) ,
@@ -40,6 +47,7 @@ impl Drop for StopSource {
40
47
}
41
48
42
49
pin_project ! {
50
+ /// StopToken is a future which completes when the associated [StopSource] is dropped.
43
51
#[ derive( Debug ) ]
44
52
pub struct StopToken {
45
53
#[ pin]
@@ -51,6 +59,7 @@ pin_project! {
51
59
}
52
60
53
61
impl StopToken {
62
+ /// Produce a StopToken that associates with no [StopSource], and never completes.
54
63
pub fn never ( ) -> Self {
55
64
let event = Event :: new ( ) ;
56
65
Self {
@@ -86,6 +95,9 @@ impl Future for StopToken {
86
95
}
87
96
88
97
pin_project ! {
98
+ /// A stream that early exits when inner [StopToken] completes.
99
+ ///
100
+ /// Users usually do not need to construct this type manually, but rather use the [StopStreamExt::stop_on] method instead.
89
101
#[ derive( Debug ) ]
90
102
pub struct StopStream <S > {
91
103
#[ pin]
@@ -96,6 +108,7 @@ pin_project! {
96
108
}
97
109
98
110
impl < S > StopStream < S > {
111
+ /// Wraps a stream to exit early when `stop_token` completes.
99
112
pub fn new ( stream : S , stop_token : StopToken ) -> Self {
100
113
Self { stream, stop_token }
101
114
}
@@ -117,7 +130,9 @@ where
117
130
}
118
131
}
119
132
133
+ /// Stream extensions to generate [StopStream] that exits early when `stop_token` completes.
120
134
pub trait StopStreamExt : Sized {
135
+ /// Wraps a stream to exit early when `stop_token` completes.
121
136
fn stop_on ( self , stop_token : StopToken ) -> StopStream < Self > {
122
137
StopStream :: new ( self , stop_token)
123
138
}
@@ -126,6 +141,9 @@ pub trait StopStreamExt: Sized {
126
141
impl < S > StopStreamExt for S where S : Stream { }
127
142
128
143
pin_project ! {
144
+ /// A future that early exits when inner [StopToken] completes.
145
+ ///
146
+ /// Users usually do not need to construct this type manually, but rather use the [StopFutureExt::stop_on] method instead.
129
147
#[ derive( Debug ) ]
130
148
pub struct StopFuture <F > {
131
149
#[ pin]
@@ -136,6 +154,7 @@ pin_project! {
136
154
}
137
155
138
156
impl < F > StopFuture < F > {
157
+ /// Wraps a future to exit early when `stop_token` completes.
139
158
pub fn new ( future : F , stop_token : StopToken ) -> Self {
140
159
Self { future, stop_token }
141
160
}
@@ -160,7 +179,9 @@ where
160
179
}
161
180
}
162
181
182
+ /// Future extensions to generate [StopFuture] that exits early when `stop_token` completes.
163
183
pub trait StopFutureExt : Sized {
184
+ /// Wraps a future to exit early when `stop_token` completes.
164
185
fn stop_on ( self , stop_token : StopToken ) -> StopFuture < Self > {
165
186
StopFuture :: new ( self , stop_token)
166
187
}
0 commit comments