1use std::sync::{atomic, Arc};
2use std::time::UNIX_EPOCH;
3
4
5pub trait Clock: Send + Sync + 'static {
6 fn now_unix_nano(&self) -> u64;
7}
8
9pub struct StdClock;
10
11impl Clock for StdClock {
12 fn now_unix_nano(&self) -> u64 {
13 std::time::SystemTime::now()
14 .duration_since(UNIX_EPOCH).unwrap()
15 .as_nanos() as u64
16 }
17}
18
19#[derive(Default, Clone)]
20pub struct TestClock(Arc<atomic::AtomicU64>);
21
22impl TestClock {
23 pub fn advance(&self, by: u64){
24 self.0.fetch_add(by, atomic::Ordering::SeqCst);
25 }
26}
27
28impl Clock for TestClock {
29 fn now_unix_nano(&self) -> u64 {
30 self.0.load(atomic::Ordering::SeqCst)
31 }
32}