futures_time/task/
sleep_until.rs1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use pin_project_lite::pin_project;
6
7use crate::time::Instant;
8
9#[cfg(feature = "web")]
10use crate::task::web_timer::Timer;
11#[cfg(not(feature = "web"))]
12use async_io::Timer;
13
14pub fn sleep_until(deadline: Instant) -> SleepUntil {
16 SleepUntil {
17 timer: Timer::at(deadline.into()),
18 completed: false,
19 }
20}
21
22pin_project! {
23 #[must_use = "futures do nothing unless polled or .awaited"]
25 pub struct SleepUntil {
26 #[pin]
27 timer: Timer,
28 completed: bool,
29 }
30}
31
32impl Future for SleepUntil {
33 type Output = Instant;
34
35 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36 assert!(!self.completed, "future polled after completing");
37 let this = self.project();
38 match this.timer.poll(cx) {
39 Poll::Ready(instant) => {
40 *this.completed = true;
41 Poll::Ready(instant.into())
42 }
43 Poll::Pending => Poll::Pending,
44 }
45 }
46}