sisa_caching_core/clock.rs
1//! Where stores read the time, so tests can move it.
2
3use std::time::{Instant, SystemTime};
4
5/// Where stores read the time. Tests swap in a clock they can move. A store holds its clock by
6/// value and is generic over its type, so each read is a direct call. Each clone of a store holds
7/// a clone of its clock, so a clock whose time can move shares that time between its clones.
8pub trait Clock: Clone + Send + Sync + 'static {
9 /// The wall-clock time, which entries record as their creation time.
10 fn now(&self) -> SystemTime;
11
12 /// The monotonic time, which the memory store's deadlines use, so a wall-clock change
13 /// doesn't move them.
14 fn instant(&self) -> Instant;
15}
16
17/// The real clock, and the default for every store.
18#[derive(Debug, Clone, Copy, Default)]
19pub struct SystemClock;
20
21// The real time: the wall clock for creation times, the monotonic one for deadlines.
22impl Clock for SystemClock {
23 #[expect(
24 clippy::disallowed_methods,
25 reason = "This is the one sanctioned wall clock reader."
26 )]
27 // Inlined, as the memory store reads the time on every call.
28 #[inline]
29 fn now(&self) -> SystemTime {
30 SystemTime::now()
31 }
32
33 #[inline]
34 fn instant(&self) -> Instant {
35 Instant::now()
36 }
37}