safina_timer/
sleep_future.rs

1#![forbid(unsafe_code)]
2
3use crate::{schedule_wake, TimerThreadNotStarted};
4use core::future::Future;
5use core::pin::Pin;
6use core::task::{Context, Poll, Waker};
7use std::sync::{Arc, Mutex};
8use std::time::Instant;
9
10/// A future that completes after the specified time.
11///
12/// It is returned by [`sleep_until`] and [`sleep_for`].
13#[must_use = "futures stay idle unless you await them"]
14pub struct SleepFuture {
15    deadline: std::time::Instant,
16    waker: Arc<Mutex<Option<Waker>>>,
17}
18impl SleepFuture {
19    pub fn new(deadline: Instant) -> Self {
20        Self {
21            deadline,
22            waker: Arc::new(Mutex::new(None)),
23        }
24    }
25}
26impl Future for SleepFuture {
27    type Output = Result<(), TimerThreadNotStarted>;
28
29    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
30        if self.deadline < std::time::Instant::now() {
31            return Poll::Ready(Ok(()));
32        }
33        let old_waker = self.waker.lock().unwrap().replace(cx.waker().clone());
34        if old_waker.is_none() {
35            schedule_wake(self.deadline, self.waker.clone())?;
36        }
37        Poll::Pending
38    }
39}
40
41/// Returns after `deadline`.
42///
43/// # Panics
44/// Panics if [`start_timer_thread()`](fn.start_timer_thread.html) has not been called.
45/// If you need to handle this error, use [`SleepFuture::new`](struct.SleepFuture.html).
46pub async fn sleep_until(deadline: std::time::Instant) {
47    SleepFuture::new(deadline).await.unwrap();
48}
49
50/// Returns `duration` time from now.
51///
52/// # Panics
53/// Panics if [`start_timer_thread()`](fn.start_timer_thread.html) has not been called.
54/// If you need to handle this error, use [`SleepFuture::new`](struct.SleepFuture.html).
55pub async fn sleep_for(duration: core::time::Duration) {
56    SleepFuture::new(Instant::now() + duration).await.unwrap();
57}