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
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
use async_channel::{bounded, Receiver, Sender};
use futures_core::stream::Stream;
enum Never {}
#[derive(Debug)]
pub struct StopSource {
_chan: Sender<Never>,
stop_token: StopToken,
}
#[derive(Debug, Clone)]
pub struct StopToken {
chan: Receiver<Never>,
}
impl Default for StopSource {
fn default() -> StopSource {
let (sender, receiver) = bounded::<Never>(1);
StopSource {
_chan: sender,
stop_token: StopToken { chan: receiver },
}
}
}
impl StopSource {
pub fn new() -> StopSource {
StopSource::default()
}
pub fn stop_token(&self) -> StopToken {
self.stop_token.clone()
}
}
impl super::IntoDeadline for StopToken {
type Deadline = Self;
fn into_deadline(self) -> Self::Deadline {
self
}
}
impl Future for StopToken {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let chan = Pin::new(&mut self.chan);
match Stream::poll_next(chan, cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Some(never)) => match never {},
Poll::Ready(None) => Poll::Ready(()),
}
}
}