use once_cell::sync::Lazy;
use std::sync::Arc;
use std::time::Instant;
pub(crate) static SYSTEM_CLOCK: Lazy<Arc<SystemClock>> = Lazy::new(|| Arc::new(SystemClock));
pub trait Clock: 'static + Sync + Send {
fn now(&self) -> Instant;
}
pub struct SystemClock;
impl Clock for SystemClock {
#[inline]
fn now(&self) -> Instant {
Instant::now()
}
}
#[cfg(test)]
pub mod test {
use super::*;
use parking_lot::Mutex;
use std::time::Duration;
pub struct TestClock {
now: Mutex<Instant>,
}
impl TestClock {
#[allow(clippy::new_without_default)]
pub fn new() -> TestClock {
TestClock {
now: Mutex::new(Instant::now()),
}
}
pub fn advance(&self, dur: Duration) {
*self.now.lock() += dur;
}
}
impl Clock for TestClock {
fn now(&self) -> Instant {
*self.now.lock()
}
}
}