Clock Abstraction

Version 1.0.0 Updated Aug 24, 2026

Monotony exposes a narrow MonotonicClock trait over std::time::Instant. Inject the trait into code that measures durations, so production code can use StdMonotonicClock while tests provide deterministic time.

use std::time::Duration;

use monotony::{MonotonicClock, StdMonotonicClock};

fn measure_elapsed(clock: &dyn MonotonicClock) -> Duration {
    let started_at = clock.now();
    clock.now().duration_since(started_at)
}

let elapsed = measure_elapsed(&StdMonotonicClock);

assert!(elapsed >= Duration::ZERO);

Import MonotonicClockExt when code wants the common elapsed-time helper without changing the core trait contract.

use std::time::{Duration, Instant};

use monotony::{MonotonicClock, MonotonicClockExt};

fn has_elapsed(clock: &dyn MonotonicClock, started_at: Instant) -> bool {
    clock.elapsed_since(started_at) >= Duration::from_secs(5)
}