asyncio/
clock.rs

1use core::Expiry;
2
3use std::ops::Add;
4use std::time::{Duration, SystemTime, Instant};
5
6pub trait Clock : Send + 'static {
7    type Duration;
8
9    type TimePoint : Add<Self::Duration, Output = Self::TimePoint> + Into<Expiry>;
10
11    fn now() -> Self::TimePoint;
12}
13
14/// Provides a monotonic clock.
15pub struct SteadyClock;
16
17impl Clock for SteadyClock {
18    type Duration = Duration;
19
20    type TimePoint = Instant;
21
22    fn now() -> Self::TimePoint {
23        Instant::now()
24    }
25}
26
27/// Provides a real-time clock.
28pub struct SystemClock;
29
30impl Clock for SystemClock {
31    type Duration = Duration;
32
33    type TimePoint = SystemTime;
34
35    fn now() -> Self::TimePoint {
36        SystemTime::now()
37    }
38}