1use core::time::Duration;
4
5pub trait Clock {
10 type Instant: Copy + PartialOrd;
12
13 fn now(&self) -> Self::Instant;
15 fn elapsed(&self, earlier: Self::Instant) -> Duration;
17 fn add(&self, instant: Self::Instant, dur: Duration) -> Self::Instant;
19}
20
21#[cfg(feature = "std")]
22#[derive(Clone, Copy, Debug, Default)]
24pub struct StdClock;
25
26#[cfg(feature = "std")]
27impl Clock for StdClock {
28 type Instant = std::time::Instant;
29
30 fn now(&self) -> Self::Instant {
32 std::time::Instant::now()
33 }
34
35 fn elapsed(&self, earlier: Self::Instant) -> Duration {
37 earlier.elapsed()
38 }
39
40 fn add(&self, instant: Self::Instant, dur: Duration) -> Self::Instant {
42 instant.checked_add(dur).unwrap_or(instant)
43 }
44}