pub struct ManualClock { /* private fields */ }Expand description
A clock whose time only moves when the test moves it.
The C++ header names “freeze or step time in tests without sleeping” as a reason the abstraction exists, but leaves each test to build its own stub. Providing one here keeps the scene, cursor, and pacing suites from reinventing it — and keeps them from reinventing it differently.
Interior mutability is deliberate: Clock::now takes &self, and a test
holding a &dyn Clock still needs to advance it.
use core::time::Duration;
use drmkit_time::{Clock, ManualClock, Timestamp};
let clock = ManualClock::new();
assert_eq!(clock.now(), Timestamp::ZERO);
clock.advance(Duration::from_millis(16));
assert_eq!(clock.now(), Timestamp::from_nanos(16_000_000));
clock.set(Timestamp::from_nanos(1_000));
assert_eq!(clock.now().as_nanos(), 1_000);Implementations§
Source§impl ManualClock
impl ManualClock
Sourcepub const fn new() -> Self
pub const fn new() -> Self
A clock frozen at Timestamp::ZERO.
Sourcepub const fn starting_at(start: Timestamp) -> Self
pub const fn starting_at(start: Timestamp) -> Self
A clock frozen at start.
Sourcepub fn advance(&self, delta: Duration)
pub fn advance(&self, delta: Duration)
Move the clock forward by delta. Saturates at u64::MAX nanoseconds.
Saturating rather than wrapping is the whole point: a wrapped counter
runs backwards, and every interval taken against it collapses to
Duration::ZERO (see Timestamp::saturating_duration_since) —
a test clock that silently stalls pacing instead of failing loudly.