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
use crate::time::{Clock, Duration, Instant};
#[derive(Debug)]
pub(crate) struct TimeSource {
start_time: Instant,
}
impl TimeSource {
pub(crate) fn new(clock: &Clock) -> Self {
Self {
start_time: clock.now(),
}
}
pub(crate) fn deadline_to_tick(&self, t: Instant) -> u64 {
self.instant_to_tick(t + Duration::from_nanos(999_999))
}
pub(crate) fn instant_to_tick(&self, t: Instant) -> u64 {
let dur: Duration = t
.checked_duration_since(self.start_time)
.unwrap_or_else(|| Duration::from_secs(0));
let ms = dur.as_millis();
ms.try_into().unwrap_or(u64::MAX)
}
pub(crate) fn tick_to_duration(&self, t: u64) -> Duration {
Duration::from_millis(t)
}
pub(crate) fn now(&self, clock: &Clock) -> u64 {
self.instant_to_tick(clock.now())
}
}