Clock

Trait Clock 

Source
pub trait Clock {
    // Required method
    fn now_ms(&self) -> u64;

    // Provided method
    fn poll_delay_until(
        &self,
        cx: &mut Context<'_>,
        deadline_ms: u64,
    ) -> Poll<()> { ... }
}
Expand description

Monotonic millisecond clock.

Required Methods§

Source

fn now_ms(&self) -> u64

Return milliseconds since an arbitrary monotonic epoch.

Provided Methods§

Source

fn poll_delay_until(&self, cx: &mut Context<'_>, deadline_ms: u64) -> Poll<()>

Poll a delay that completes when the monotonic clock reaches deadline_ms.

Returns Poll::Ready(()) if the deadline has already passed. Otherwise the implementation MUST register cx.waker() with a platform timer and return Poll::Pending, so the task is woken when the deadline elapses.

The default implementation has no real timer: it schedules an immediate re-poll (via cx.waker().wake_by_ref()) and returns Poll::Pending, causing the caller to busy-poll until the monotonic clock reaches the deadline. This is correct but wastes CPU; platform clocks backed by a real timer (tokio, embassy, etc.) MUST override this to sleep efficiently. A default that returned Ready unconditionally would spin just as hard; one that returned Pending without waking would stall timer-driven work entirely.

Implementors§