use std::time::Instant;
pub(super) trait Clock: std::fmt::Debug + Send + Sync {
fn now(&self) -> Instant;
}
#[derive(Debug, Clone, Copy, Default)]
pub(super) struct SystemClock;
impl Clock for SystemClock {
fn now(&self) -> Instant {
Instant::now()
}
}
#[cfg(test)]
#[derive(Debug)]
pub(super) struct FakeClock {
now: std::sync::Mutex<Instant>,
}
#[cfg(test)]
impl FakeClock {
pub(super) fn new() -> Self {
Self {
now: std::sync::Mutex::new(Instant::now()),
}
}
pub(super) fn advance(&self, duration: std::time::Duration) {
let mut now = crate::bridge::lock_std(&self.now);
*now += duration;
}
}
#[cfg(test)]
impl Clock for FakeClock {
fn now(&self) -> Instant {
*crate::bridge::lock_std(&self.now)
}
}