vexide_async/time.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//! Utilities for tracking time.
//!
//! This module provides a types for executing code after a set period of time.
//!
//! * [`Sleep`] is a future that does no work and completes at a specific [`Instant`]
//! in time.
//!
//! * [`sleep`] and [`sleep_until`] provide ways to yield control away from a future
//! for or until a specific instant in time.
use core::{
future::Future,
pin::Pin,
task::{Context, Poll},
time::Duration,
};
use vexide_core::time::Instant;
use crate::executor::EXECUTOR;
/// A future that will complete after a certain instant is reached in time.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Sleep(Instant);
impl Future for Sleep {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> core::task::Poll<Self::Output> {
if Instant::now() > self.0 {
Poll::Ready(())
} else {
EXECUTOR.with_reactor(|reactor| reactor.sleepers.push(cx.waker().clone(), self.0));
Poll::Pending
}
}
}
/// Returns a future that will complete after the given duration.
pub fn sleep(duration: Duration) -> Sleep {
Sleep(Instant::now() + duration)
}
/// Returns a future that waits until a deadline is reached.
pub const fn sleep_until(deadline: Instant) -> Sleep {
Sleep(deadline)
}