1use core::{
12 future::Future,
13 pin::Pin,
14 task::{Context, Poll},
15 time::Duration,
16};
17
18use vexide_core::time::Instant;
19
20use crate::executor::EXECUTOR;
21
22#[derive(Debug)]
24#[must_use = "futures do nothing unless you `.await` or poll them"]
25pub struct Sleep(Instant);
26
27impl Future for Sleep {
28 type Output = ();
29
30 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> core::task::Poll<Self::Output> {
31 if Instant::now() > self.0 {
32 Poll::Ready(())
33 } else {
34 EXECUTOR.with_reactor(|reactor| reactor.sleepers.push(cx.waker().clone(), self.0));
35
36 Poll::Pending
37 }
38 }
39}
40
41pub fn sleep(duration: Duration) -> Sleep {
43 Sleep(Instant::now() + duration)
44}
45
46pub const fn sleep_until(deadline: Instant) -> Sleep {
48 Sleep(deadline)
49}