Skip to content

Commit a166193

Browse files
author
rusty
committed
Add documentations for cancellation module
1 parent 797ca28 commit a166193

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/cancellation.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Future and Stream cancellation
2+
13
use std::pin::Pin;
24
use std::sync::atomic::{AtomicBool, Ordering};
35

@@ -9,20 +11,25 @@ use async_std::task::{Context, Poll};
911
use event_listener::{Event, EventListener};
1012
use pin_project_lite::pin_project;
1113

14+
/// StopSource produces [StopToken] and cancels all of its tokens on drop.
1215
#[derive(Debug)]
1316
pub struct StopSource {
1417
stopped: Arc<AtomicBool>,
1518
event: Arc<Event>,
1619
}
1720

1821
impl StopSource {
22+
/// Create a new StopSource
1923
pub fn new() -> Self {
2024
Self {
2125
stopped: Arc::new(AtomicBool::new(false)),
2226
event: Arc::new(Event::new()),
2327
}
2428
}
2529

30+
/// Produce a new [StopToken], associated with this source.
31+
///
32+
/// Once this source is dropped, all associated [StopToken] futures will complete.
2633
pub fn token(&self) -> StopToken {
2734
StopToken {
2835
stopped: self.stopped.clone(),
@@ -40,6 +47,7 @@ impl Drop for StopSource {
4047
}
4148

4249
pin_project! {
50+
/// StopToken is a future which completes when the associated [StopSource] is dropped.
4351
#[derive(Debug)]
4452
pub struct StopToken {
4553
#[pin]
@@ -51,6 +59,7 @@ pin_project! {
5159
}
5260

5361
impl StopToken {
62+
/// Produce a StopToken that associates with no [StopSource], and never completes.
5463
pub fn never() -> Self {
5564
let event = Event::new();
5665
Self {
@@ -86,6 +95,9 @@ impl Future for StopToken {
8695
}
8796

8897
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.
89101
#[derive(Debug)]
90102
pub struct StopStream<S> {
91103
#[pin]
@@ -96,6 +108,7 @@ pin_project! {
96108
}
97109

98110
impl<S> StopStream<S> {
111+
/// Wraps a stream to exit early when `stop_token` completes.
99112
pub fn new(stream: S, stop_token: StopToken) -> Self {
100113
Self { stream, stop_token }
101114
}
@@ -117,7 +130,9 @@ where
117130
}
118131
}
119132

133+
/// Stream extensions to generate [StopStream] that exits early when `stop_token` completes.
120134
pub trait StopStreamExt: Sized {
135+
/// Wraps a stream to exit early when `stop_token` completes.
121136
fn stop_on(self, stop_token: StopToken) -> StopStream<Self> {
122137
StopStream::new(self, stop_token)
123138
}
@@ -126,6 +141,9 @@ pub trait StopStreamExt: Sized {
126141
impl<S> StopStreamExt for S where S: Stream {}
127142

128143
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.
129147
#[derive(Debug)]
130148
pub struct StopFuture<F> {
131149
#[pin]
@@ -136,6 +154,7 @@ pin_project! {
136154
}
137155

138156
impl<F> StopFuture<F> {
157+
/// Wraps a future to exit early when `stop_token` completes.
139158
pub fn new(future: F, stop_token: StopToken) -> Self {
140159
Self { future, stop_token }
141160
}
@@ -160,7 +179,9 @@ where
160179
}
161180
}
162181

182+
/// Future extensions to generate [StopFuture] that exits early when `stop_token` completes.
163183
pub trait StopFutureExt: Sized {
184+
/// Wraps a future to exit early when `stop_token` completes.
164185
fn stop_on(self, stop_token: StopToken) -> StopFuture<Self> {
165186
StopFuture::new(self, stop_token)
166187
}

0 commit comments

Comments
 (0)