Skip to main content

SimClock

Struct SimClock 

Source
pub struct SimClock { /* private fields */ }
Expand description

A clock and a delay on simulated time.

now starts at zero and moves only when something advances it: a delay on the clock, or the test, with advance. It never reads the host’s time, never moves backwards, and saturates instead of overflowing.

Two settings make it behave like a coarser or slower platform:

  • a tick (with_tick): now reads in whole ticks, rounded down, and a delay is rounded up to whole ticks, as hdmi-hal’s Delay allows;
  • an overshoot (with_overshoot): every delay waits that much longer than asked, as a real delay may, so deadline arithmetic is tested against it.

now read after delay(d) is always at least d later than before it.

The time sits behind a critical_section::Mutex, so the clock is Sync: one clock can be shared between threads and held across an .await in Send code. A host needs the std feature (or another critical-section implementation); a bare target supplies its own.

Clock and Delay are implemented for the clock and for a reference to it, so one clock can be passed as both where a procedure takes them as separate values:

use core::time::Duration;
use maquette::{Clock, Delay, SimClock, Timestamp};

fn wait_for<C: Clock, D: Delay>(clock: &C, delay: &mut D, d: Duration) -> Duration {
    let start = clock.now();
    delay.delay(d);
    clock.now().saturating_duration_since(start)
}

let clock = SimClock::new().with_overshoot(Duration::from_micros(250));
let waited = wait_for(&clock, &mut &clock, Duration::from_millis(1));
assert_eq!(waited, Duration::from_micros(1_250));

clock.advance(Duration::from_millis(10));
assert_eq!(clock.now(), Timestamp::from_micros(11_250));

Implementations§

Source§

impl SimClock

Source

pub const fn new() -> Self

A clock at zero with a 1 µs tick and no overshoot.

Source

pub const fn with_tick(self, tick: Duration) -> Self

The clock with a resolution of tick, rounded up to whole microseconds; zero means 1 µs, the finest a Timestamp holds.

Source

pub const fn with_overshoot(self, overshoot: Duration) -> Self

The clock with every delay waiting overshoot longer than asked, rounded up to whole microseconds.

Source

pub const fn tick(&self) -> Duration

The clock’s resolution.

Source

pub const fn overshoot(&self) -> Duration

How much longer than asked every delay waits.

Source

pub fn advance(&self, duration: Duration)

Moves the clock duration forward, rounded up to whole microseconds; it stops at the largest time a Timestamp holds.

Trait Implementations§

Source§

impl Clock for SimClock

Source§

fn now(&self) -> Timestamp

The simulated time, rounded down to the tick.

Source§

impl Clock for &SimClock

Source§

fn now(&self) -> Timestamp

The current time. Never earlier than any time this clock returned before.
Source§

impl Debug for SimClock

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SimClock

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Delay for SimClock

Source§

fn delay(&mut self, duration: Duration)

Moves the clock duration forward, rounded up to whole ticks, plus the overshoot.

A whole number of ticks moves now, which reads in ticks, by exactly that much, so now read after the delay is at least duration later than before it.

Source§

impl Delay for &SimClock

Source§

fn delay(&mut self, duration: Duration)

Wait at least duration, rounding up to the implementation’s own tick; never less, so a minimum wait holds whatever the timer. Read more
Source§

impl SendDelay for SimClock

Available on crate feature async only.

A simulated clock’s delay, for an async procedure that needs only time: no bench.

Source§

async fn delay(&mut self, duration: Duration)

Source§

impl SendDelay for &SimClock

Available on crate feature async only.

As for SimClock, through a shared reference, as the sync Delay is.

Source§

async fn delay(&mut self, duration: Duration)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Delay for T
where T: SendDelay,

Source§

fn delay(&mut self, duration: Duration) -> impl Future<Output = ()>

Wait at least duration, rounding up to the implementation’s own tick; never less, so a minimum wait holds whatever the timer. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.