Skip to main content

ManualClock

Struct ManualClock 

Source
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

Source

pub const fn new() -> Self

A clock frozen at Timestamp::ZERO.

Source

pub const fn starting_at(start: Timestamp) -> Self

A clock frozen at start.

Source

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.

Source

pub fn set(&self, at: Timestamp)

Jump the clock to at.

Nothing stops a test from moving it backwards — that is the point of a manual clock — but production code must never observe a clock that does, so keep such tests scoped to the code under test.

Trait Implementations§

Source§

impl Clock for ManualClock

Source§

fn now(&self) -> Timestamp

Sample the clock.
Source§

impl Debug for ManualClock

Source§

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

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

impl Default for ManualClock

Source§

fn default() -> Self

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

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> 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.