async_std/future/future/
delay.rs1use std::future::Future;
2use std::pin::Pin;
3use std::time::Duration;
4
5use pin_project_lite::pin_project;
6
7use crate::task::{Context, Poll};
8use crate::utils::{timer_after, Timer};
9
10pin_project! {
11 #[doc(hidden)]
12 #[allow(missing_debug_implementations)]
13 pub struct DelayFuture<F> {
14 #[pin]
15 future: F,
16 #[pin]
17 delay: Timer,
18 }
19}
20
21impl<F> DelayFuture<F> {
22 pub fn new(future: F, dur: Duration) -> DelayFuture<F> {
23 let delay = timer_after(dur);
24
25 DelayFuture { future, delay }
26 }
27}
28
29impl<F: Future> Future for DelayFuture<F> {
30 type Output = F::Output;
31
32 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
33 let this = self.project();
34
35 match this.delay.poll(cx) {
36 Poll::Pending => Poll::Pending,
37 Poll::Ready(_) => match this.future.poll(cx) {
38 Poll::Ready(v) => Poll::Ready(v),
39 Poll::Pending => Poll::Pending,
40 },
41 }
42 }
43}