Skip to main content

TimeSource

Trait TimeSource 

Source
pub trait TimeSource:
    Send
    + Sync
    + Debug {
    // Required methods
    fn now(&self) -> Instant;
    fn sleep(&self, duration: Duration);
    fn today_date(&self) -> NaiveDate;

    // Provided method
    fn elapsed_since(&self, earlier: Instant) -> Duration { ... }
}
Expand description

Abstraction over time-related operations.

This trait allows production code to use real system time while tests can use a controllable mock implementation for fast, deterministic testing.

Required Methods§

Source

fn now(&self) -> Instant

Get the current instant for measuring elapsed time.

Source

fn sleep(&self, duration: Duration)

Sleep for the specified duration.

In tests, this may be a no-op or advance logical time.

Source

fn today_date(&self) -> NaiveDate

Get today’s date.

Provided Methods§

Source

fn elapsed_since(&self, earlier: Instant) -> Duration

Calculate elapsed time since an earlier instant.

Implementors§