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
use crate::{deadline::TimedOutError, IntoDeadline};
use core::future::Future;
use core::pin::Pin;
use pin_project_lite::pin_project;
use std::task::{Context, Poll};
pub trait FutureExt: Future {
fn until<T, D>(self, target: T) -> Stop<Self, D>
where
Self: Sized,
T: IntoDeadline<Deadline = D>,
{
Stop {
deadline: target.into_deadline(),
future: self,
}
}
}
pin_project! {
#[must_use = "Futures do nothing unless polled or .awaited"]
#[derive(Debug)]
pub struct Stop<F, D> {
#[pin]
future: F,
#[pin]
deadline: D,
}
}
impl<F, D> Future for Stop<F, D>
where
F: Future,
D: Future<Output = ()>,
{
type Output = Result<F::Output, TimedOutError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if let Poll::Ready(()) = this.deadline.poll(cx) {
return Poll::Ready(Err(TimedOutError::new()));
}
match this.future.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(it) => Poll::Ready(Ok(it)),
}
}
}