Skip to main content

Clock

Trait Clock 

Source
pub trait Clock {
    // Required method
    fn now(&self) -> Instant;
}
Expand description

A monotonic source of time.

Declared by the engine, which is its only consumer, and consumed through impl Clock so the call monomorphises. One method: the engine reads the clock and does the arithmetic itself, because “how long is a sampling request allowed to take” is a session decision and a timer that knows the policy would be a policy in an adapter.

§Examples

use std::time::Duration;
use protocol_droid::application::session::Clock;
use protocol_droid::domain::session::Instant;

/// A clock the test drives by hand.
struct TestClock(std::sync::atomic::AtomicU64);

impl Clock for TestClock {
    fn now(&self) -> Instant {
        let millis = self.0.load(std::sync::atomic::Ordering::Relaxed);
        Instant::from_duration(Duration::from_millis(millis))
    }
}

let clock = TestClock(std::sync::atomic::AtomicU64::new(1_000));
assert_eq!(clock.now().as_duration(), Duration::from_secs(1));

Required Methods§

Source

fn now(&self) -> Instant

The current reading.

Only the difference between two readings of the same clock is meaningful; the origin carries no information.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§