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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use futures::future::FusedFuture;
use futures_intrusive::channel::shared::{state_broadcast_channel, StateReceiver, StateSender};
use futures_intrusive::sync::ManualResetEvent;
use pin_project::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use crate::with_token::WithToken;
use crate::{cancellation, Canceled, Cancellation, Join, Token};
fn make_join() -> (Arc<Join>, Arc<ManualResetEvent>) {
let event = Arc::new(ManualResetEvent::new(false));
(
Arc::new(Join {
event: event.clone(),
}),
event,
)
}
enum State<Fut>
where
Fut: Future,
{
Running {
cancel: Arc<StateReceiver<bool>>,
join: Arc<Join>,
},
Joining(Fut::Output),
Done,
}
#[must_use = "futures do nothing unless polled"]
#[pin_project]
pub struct ScopeFuture<Fut, Joiner>
where
Fut: Future,
{
state: State<Fut>,
#[pin]
cancellation: Cancellation,
#[pin]
inner: WithToken<Fut>,
#[pin]
joiner: Joiner,
cancel_sender: Option<StateSender<bool>>,
}
impl<Fut, Joiner> ScopeFuture<Fut, Joiner>
where
Fut: Future,
{
pub fn cancel(self: Pin<&mut Self>) {
if let Some(cancel_sender) = &self.cancel_sender {
let _ = cancel_sender.send(true);
}
}
pub fn force_cancel(self: Pin<&mut Self>) {
self.project().cancel_sender.take();
}
}
impl<Fut, Joiner> Future for ScopeFuture<Fut, Joiner>
where
Fut: Future,
Joiner: Future<Output = ()>,
{
type Output = Result<Fut::Output, Canceled>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.as_mut().project();
match this.cancellation.poll(cx) {
Poll::Ready(Some(Canceled::Graceful)) => self.as_mut().cancel(),
Poll::Ready(Some(Canceled::Forced)) => {
let ret = match std::mem::replace(this.state, State::Done) {
State::Joining(v) => Poll::Ready(Ok(v)),
_ => Poll::Ready(Err(Canceled::Forced)),
};
self.force_cancel();
return ret;
}
_ => {}
}
let this = self.project();
match this.state {
State::Done => panic!("poll after completion or forced cancellation"),
State::Running { cancel, join } => {
let token = Token {
cancel: cancel.clone(),
join: Arc::downgrade(join),
};
match this.inner.poll(cx, token) {
Poll::Pending => return Poll::Pending,
Poll::Ready(v) => {
*this.state = State::Joining(v);
}
}
}
_ => {}
}
match this.joiner.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(()) => match std::mem::replace(this.state, State::Done) {
State::Joining(v) => Poll::Ready(Ok(v)),
_ => unreachable!(),
},
}
}
}
impl<Fut, Joiner> FusedFuture for ScopeFuture<Fut, Joiner>
where
Fut: Future,
Joiner: Future<Output = ()>,
{
fn is_terminated(&self) -> bool {
match self.state {
State::Done => true,
_ => false,
}
}
}
pub fn scope<Fut>(
inner: Fut,
) -> ScopeFuture<impl Future<Output = Fut::Output>, impl Future<Output = ()>>
where
Fut: Future,
{
let (cancel_sender, receiver) = state_broadcast_channel();
let (join, event) = make_join();
let joiner = async move {
event.wait().await;
};
ScopeFuture {
state: State::Running {
cancel: Arc::new(receiver),
join,
},
cancellation: cancellation(),
inner: WithToken::new(inner),
joiner,
cancel_sender: Some(cancel_sender),
}
}