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):nowreads in whole ticks, rounded down, and a delay is rounded up to whole ticks, as hdmi-hal’sDelayallows; - 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
impl SimClock
Sourcepub const fn with_tick(self, tick: Duration) -> Self
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.
Sourcepub const fn with_overshoot(self, overshoot: Duration) -> Self
pub const fn with_overshoot(self, overshoot: Duration) -> Self
The clock with every delay waiting overshoot longer than asked, rounded up to whole
microseconds.