1use std::future::Future;
2use std::pin::Pin;
3use std::task::{ready, Context, Poll};
4use std::time::Duration;
5
6use futures_util::future::BoxFuture;
7use futures_util::FutureExt as _;
8
9pub struct Delay {
10 duration: Duration,
11 inner: BoxFuture<'static, ()>,
12}
13
14impl Delay {
15 #[cfg(feature = "tokio")]
16 pub fn tokio(duration: Duration) -> Self {
17 Self {
18 duration,
19 inner: tokio::time::sleep(duration).boxed(),
20 }
21 }
22
23 #[cfg(feature = "futures-timer")]
24 pub fn futures_timer(duration: Duration) -> Self {
25 Self {
26 duration,
27 inner: futures_timer::Delay::new(duration).boxed(),
28 }
29 }
30}
31
32impl Future for Delay {
33 type Output = Duration;
34
35 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36 let this = self.get_mut();
37
38 ready!(this.inner.poll_unpin(cx));
39
40 Poll::Ready(this.duration)
41 }
42}