Skip to main content

ManualClock

Struct ManualClock 

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

A clock whose time is set explicitly, for deterministic tests.

Uses interior mutability so now, set, and advance all take &self.

Implementations§

Source§

impl ManualClock

Source

pub const fn new(start: u64) -> Self

Creates a clock reading start.

Examples found in repository?
examples/basic.rs (line 11)
9fn main() {
10    // Deterministic clock for tests / demos.
11    let clock = ManualClock::new(0);
12    println!("manual t = {}", clock.now());
13    clock.advance(1_000);
14    println!("after advance(1000): t = {}", clock.now());
15
16    // Real monotonic clock (milliseconds since creation).
17    let real = MonotonicClock::new();
18    let a = real.now();
19    let b = real.now();
20    println!("monotonic: {a} -> {b} (non-decreasing: {})", b >= a);
21
22    // Any &Clock works wherever a Clock is expected.
23    println!("via trait object: {}", read(&clock));
24}
Source

pub fn set(&self, tick: u64)

Sets the clock to tick.

Source

pub fn advance(&self, delta: u64)

Advances the clock by delta (saturating).

Examples found in repository?
examples/basic.rs (line 13)
9fn main() {
10    // Deterministic clock for tests / demos.
11    let clock = ManualClock::new(0);
12    println!("manual t = {}", clock.now());
13    clock.advance(1_000);
14    println!("after advance(1000): t = {}", clock.now());
15
16    // Real monotonic clock (milliseconds since creation).
17    let real = MonotonicClock::new();
18    let a = real.now();
19    let b = real.now();
20    println!("monotonic: {a} -> {b} (non-decreasing: {})", b >= a);
21
22    // Any &Clock works wherever a Clock is expected.
23    println!("via trait object: {}", read(&clock));
24}

Trait Implementations§

Source§

impl Clock for ManualClock

Source§

fn now(&self) -> u64

Returns the current time as a u64 tick.
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() -> ManualClock

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.