safina_timer/
sleep_future.rs1#![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#[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
41pub async fn sleep_until(deadline: std::time::Instant) {
47 SleepFuture::new(deadline).await.unwrap();
48}
49
50pub async fn sleep_for(duration: core::time::Duration) {
56 SleepFuture::new(Instant::now() + duration).await.unwrap();
57}