-
Notifications
You must be signed in to change notification settings - Fork 604
/
on_poll.rs
279 lines (257 loc) · 8.35 KB
/
on_poll.rs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use super::assert_future;
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
use futures_core::task::{Context, Poll};
use pin_project_lite::pin_project;
pin_project! {
/// Future for the [`on_poll`] function.
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
pub struct OnPoll<T, F> {
#[pin]
future: Option<T>,
function: Option<F>,
}
}
/// Wrapps a future and a function in an [`OnPoll`]. On each [poll](core::future::Future::poll())
/// the wrapped future is polled internally and the function is invoked.
///
/// The function provides mutable access to the future's [`Context`] and to its [`Poll`].
///
/// After the internal future has returned [`Poll::Ready`] additional polls to an [`OnPoll`]
/// will lead to a panic.
///
/// **Warning:** You should only call the future's waker in this function when it is safe to
/// poll the future another time. Otherwise this might lead to a panic.
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future::on_poll;
/// use futures::pending;
/// use futures::task::{Context, Poll};
/// let mut x = 0;
/// let future = async {
/// pending!(); // returns Poll::Pending, so x = 1
/// pending!(); // returns Poll::Pending, so x = 2
/// pending!(); // returns Poll::Pending, so x = 3
/// "Some future" // returns Poll::Ready, so x = 4
/// };
/// futures::pin_mut!(future);
/// let funct = |cx : &mut Context<'_>, poll : &mut Poll<_>| {
/// x += 1;
/// if let Poll::Pending = poll {
/// cx.waker().wake_by_ref(); // only to let the future make progress
/// }
/// };
/// let res = on_poll(future, funct).await;
/// assert_eq!(x, 4);
/// assert_eq!(res, "Some future");
/// # })
/// ```
pub fn on_poll<T, F>(future: T, function: F) -> OnPoll<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut Poll<T::Output>),
{
assert_future::<T::Output, _>(OnPoll { future: Some(future), function: Some(function) })
}
impl<T, F> Future for OnPoll<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut Poll<T::Output>),
{
type Output = T::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
let future =
this.future.as_mut().as_pin_mut().expect("cannot poll OnPoll after it was ready");
let mut poll = future.poll(cx);
if let Poll::Ready(_) = poll {
this.future.set(None);
unwrap_option(this.function.take())(cx, &mut poll);
return poll;
}
unwrap_option(this.function.as_mut())(cx, &mut poll);
poll
}
}
impl<T, F> FusedFuture for OnPoll<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut Poll<T::Output>),
{
fn is_terminated(&self) -> bool {
self.future.is_none()
}
}
pin_project! {
/// Future for the [`on_poll_pending`] function.
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
pub struct OnPollPending<T, F> {
#[pin]
future: Option<T>,
function: Option<F>,
}
}
/// Wrapps a future and a function in an [`OnPollPending`]. On each [poll](core::future::Future::poll())
/// the wrapped future is polled internally. The function is invoked each time the internal future returns
/// [`Poll::Pending`].
///
/// The function provides mutable access to the future's [`Context`] and to its [`Poll`].
///
/// After the internal future has returned [`Poll::Ready`] additional polls to an [`OnPollPending`]
/// will lead to a panic.
///
/// Can be used to abort a future after a specific number of pending polls by changing its [`Poll`]
/// to [`Poll::Ready`].
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future::on_poll_pending;
/// use futures::pending;
/// use futures::task::{Context, Poll};
/// let mut x = 0;
/// let future = async {
/// pending!(); // returns Poll::Pending ,so x = 1
/// pending!(); // returns Poll::Pending, so x = 2
/// pending!(); // returns Poll::Pending, so x = 3
/// "Some future" // returns Poll::Ready, so x is unchanged
/// };
/// futures::pin_mut!(future);
/// let funct = |cx : &mut Context<'_>, poll : &mut Poll<_>| {
/// *&mut x += 1;
/// cx.waker().wake_by_ref(); // only to let the future make progress
/// };
/// let res = on_poll_pending(future, funct).await;
/// assert_eq!(x, 3);
/// assert_eq!(res, "Some future");
/// # })
/// ```
pub fn on_poll_pending<T, F>(future: T, function: F) -> OnPollPending<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut Poll<T::Output>),
{
assert_future::<T::Output, _>(OnPollPending { future: Some(future), function: Some(function) })
}
impl<T, F> Future for OnPollPending<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut Poll<T::Output>),
{
type Output = T::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
let future = this
.future
.as_mut()
.as_pin_mut()
.expect("cannot poll OnPollPending after it was ready");
let mut poll = future.poll(cx);
if let Poll::Ready(_) = poll {
this.future.set(None);
unwrap_option(this.function.take());
return poll;
}
unwrap_option(this.function.as_mut())(cx, &mut poll);
// Function was called so we need to check if the poll was manipulated to Poll::Ready
if let Poll::Ready(_) = poll {
this.future.set(None);
unwrap_option(this.function.take());
}
poll
}
}
impl<T, F> FusedFuture for OnPollPending<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut Poll<T::Output>),
{
fn is_terminated(&self) -> bool {
self.future.is_none()
}
}
pin_project! {
/// Future for the [`on_poll_ready`] function.
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
pub struct OnPollReady<T, F> {
#[pin]
future: Option<T>,
function: Option<F>,
}
}
/// Wrapps a future and a function in an [`OnPollReady`]. On each [poll](core::future::Future::poll())
/// the wrapped future is polled internally. The function is invoked when the internal future returns
/// [`Poll::Ready`].
///
/// The function provides mutable access to the future's [`Context`] and to its [`Output`](core::future::Future::Output).
///
/// After the internal future has returned [`Poll::Ready`] additional polls to an [`OnPollReady`]
/// will lead to a panic.
///
/// **Warning:** You should not call the waker in this function as this might lead to
/// another poll of the future. This will lead to a panic!
///
/// # Examples
///
/// ```
/// # futures::executor::block_on(async {
/// use futures::future::on_poll_ready;
/// let mut x = 0;
/// let future = async {"Some future"};
/// futures::pin_mut!(future);
///
/// let res = on_poll_ready(future, |_, _| *&mut x += 1).await;
/// assert_eq!(x, 1);
/// assert_eq!(res, "Some future");
/// # })
/// ```
pub fn on_poll_ready<T, F>(future: T, function: F) -> OnPollReady<T, F>
where
T: Future,
F: FnOnce(&mut Context<'_>, &mut T::Output),
{
assert_future::<T::Output, _>(OnPollReady { future: Some(future), function: Some(function) })
}
impl<T, F> Future for OnPollReady<T, F>
where
T: Future,
F: FnOnce(&mut Context<'_>, &mut T::Output),
{
type Output = T::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
let future =
this.future.as_mut().as_pin_mut().expect("cannot poll OnPollReady after it was ready");
if let Poll::Ready(mut val) = future.poll(cx) {
this.future.set(None);
unwrap_option(this.function.take())(cx, &mut val);
return Poll::Ready(val);
}
Poll::Pending
}
}
impl<T, F> FusedFuture for OnPollReady<T, F>
where
T: Future,
F: FnMut(&mut Context<'_>, &mut T::Output),
{
fn is_terminated(&self) -> bool {
self.future.is_none()
}
}
/// When compiled with `-C opt-level=z`, this function will help the
/// compiler eliminate the `None` branch, where `Option::unwrap` does not.
#[inline(always)]
fn unwrap_option<T>(value: Option<T>) -> T {
match value {
None => unreachable!(),
Some(value) => value,
}
}