1use crate::{CalibratedClock, Clock};
2use std::time::{Duration, Instant, SystemTime};
3
4macro_rules! std_clock {
5 ($Instant:ty,$Clock:ident,$unwrap_between:path) => {
6 #[derive(Clone, Copy, Debug)]
7 pub struct $Clock;
8
9 impl Clock for $Clock {
10 type Instant = $Instant;
11
12 fn now(self) -> Self::Instant {
13 Self::Instant::now()
14 }
15 }
16
17 impl CalibratedClock for $Clock {
18 fn between_u64_ns(self, later: Self::Instant, earlier: Self::Instant) -> u64 {
19 $unwrap_between(later.duration_since(earlier))
20 .as_nanos()
21 .try_into()
22 .unwrap()
23 }
24
25 fn add_u64_ns(self, base: Self::Instant, offset: u64) -> Self::Instant {
26 base + Duration::from_nanos(offset)
27 }
28
29 fn sub_u64_ns(self, base: Self::Instant, offset: u64) -> Self::Instant {
30 base - Duration::from_nanos(offset)
31 }
32 }
33 };
34}
35
36std_clock!(Instant, InstantClock, std::convert::identity);
37std_clock!(SystemTime, SystemClock, Result::unwrap);