sparreal_kernel/async_std/
time.rs

1use core::future::Future;
2use core::time::Duration;
3
4use crate::time::since_boot;
5
6pub fn sleep(duration: Duration) -> FutureSleep {
7    let now = since_boot();
8    FutureSleep {
9        wake_at: now + duration,
10    }
11}
12
13pub struct FutureSleep {
14    wake_at: Duration,
15}
16
17impl Future for FutureSleep {
18    type Output = ();
19
20    fn poll(
21        self: core::pin::Pin<&mut Self>,
22        cx: &mut core::task::Context<'_>,
23    ) -> core::task::Poll<Self::Output> {
24        let now = since_boot();
25        if now >= self.wake_at {
26            core::task::Poll::Ready(())
27        } else {
28            cx.waker().wake_by_ref();
29            core::task::Poll::Pending
30        }
31    }
32}