1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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()
}
}
}