Skip to main content

ManualClock

Struct ManualClock 

Source
pub struct ManualClock { /* private fields */ }
Available on crate feature std only.
Expand description

A clock under your control, for deterministic testing.

ManualClock captures the OS monotonic and wall-clock anchors at construction, then advances only when you call advance. The clock never moves on its own, so tests against time-driven code become point-in-time deterministic.

ManualClock is Send + Sync. Wrap it in an Arc to share with the code under test; both the test driver and the production code can hold references to the same clock and observe consistent readings.

§Examples

use clock_lib::{Clock, ManualClock, Monotonic};
use std::sync::Arc;
use std::time::Duration;

// A "ttl expired?" check that can be driven without sleeping.
fn expired<C: Clock>(clock: &C, stamp: Monotonic, ttl: Duration) -> bool {
    clock.now().duration_since(stamp) >= ttl
}

let clock = Arc::new(ManualClock::new());
let stamp = clock.now();

assert!(!expired(&*clock, stamp, Duration::from_secs(60)));

clock.advance(Duration::from_secs(60));
assert!(expired(&*clock, stamp, Duration::from_secs(60)));

Implementations§

Source§

impl ManualClock

Source

pub fn new() -> Self

Constructs a new manual clock anchored at the current OS time.

§Examples
use clock_lib::ManualClock;

let clock = ManualClock::new();
Source

pub fn advance(&self, by: Duration)

Advances the clock forward by by.

Successive calls accumulate. If the cumulative offset would exceed u64::MAX nanoseconds (≈584 years), the offset saturates — well outside any plausible test scenario.

§Examples
use clock_lib::{Clock, ManualClock};
use std::time::Duration;

let clock = ManualClock::new();
let a = clock.now();
clock.advance(Duration::from_secs(5));
let b = clock.now();
assert_eq!(b.duration_since(a), Duration::from_secs(5));
Source

pub fn offset(&self) -> Duration

Returns the cumulative offset that has been added to this clock since it was constructed.

§Examples
use clock_lib::ManualClock;
use std::time::Duration;

let clock = ManualClock::new();
clock.advance(Duration::from_secs(1));
clock.advance(Duration::from_secs(2));
assert_eq!(clock.offset(), Duration::from_secs(3));

Trait Implementations§

Source§

impl Clock for ManualClock

Source§

fn now(&self) -> Monotonic

Returns the monotonic anchor plus the accumulated offset.

§Panics

Panics if the anchor plus offset is not representable as an Instant on the current platform. This requires a cumulative offset measured in centuries and never occurs in realistic tests.

Source§

fn wall(&self) -> Wall

Returns the wall-clock anchor plus the accumulated offset.

§Panics

Panics if the anchor plus offset is not representable as a SystemTime on the current platform. This requires a cumulative offset measured in centuries and never occurs in realistic tests.

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