runlimit_memory/clock.rs
1use std::time::{Duration, Instant};
2
3/// Supplies monotonic elapsed time to a [`MemoryStore`](crate::MemoryStore).
4///
5/// A clock implementation must never intentionally move backwards. The store
6/// nevertheless clamps values to the greatest time observed by each touched
7/// shard, which also makes deterministic test clocks straightforward.
8pub trait Clock: Send + Sync + 'static {
9 /// Returns monotonic time elapsed from an arbitrary origin.
10 fn now(&self) -> Duration;
11}
12
13/// A monotonic clock backed by [`Instant`].
14#[derive(Clone, Debug)]
15pub struct SystemClock {
16 origin: Instant,
17}
18
19impl SystemClock {
20 /// Creates a clock whose origin is now.
21 pub fn new() -> Self {
22 Self {
23 origin: Instant::now(),
24 }
25 }
26}
27
28impl Default for SystemClock {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl Clock for SystemClock {
35 fn now(&self) -> Duration {
36 self.origin.elapsed()
37 }
38}