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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::TimerRegistry;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Instant {
duration: Duration,
timer_registry_id: u64,
}
impl Instant {
pub(crate) const fn new(duration: Duration, timer_registry_id: u64) -> Self {
Self {
duration,
timer_registry_id,
}
}
pub(crate) const fn into_duration(self, timer_registry_id: u64) -> Duration {
if self.timer_registry_id != timer_registry_id {
panic!("Can't use Instants from one TimerRegistry in another TimerRegistry.");
}
self.duration
}
pub const fn duration_since(&self, earlier: Self) -> Duration {
self.assert_instances_are_compatible(&earlier);
self.duration.saturating_sub(earlier.duration)
}
pub const fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
self.assert_instances_are_compatible(&earlier);
self.duration.checked_sub(earlier.duration)
}
pub const fn saturated_duration_since(&self, earlier: Self) -> Duration {
self.assert_instances_are_compatible(&earlier);
self.duration.saturating_sub(earlier.duration)
}
pub fn elapsed(&self, timer_registry: &TimerRegistry) -> Duration {
timer_registry.now().duration_since(*self)
}
pub const fn checked_add(&self, duration: Duration) -> Option<Self> {
let timer_registry_id = self.timer_registry_id;
match self.duration.checked_add(duration) {
Some(duration) => Some(Self {
duration,
timer_registry_id,
}),
None => None,
}
}
pub const fn checked_sub(&self, duration: Duration) -> Option<Self> {
let timer_registry_id = self.timer_registry_id;
match self.duration.checked_sub(duration) {
Some(duration) => Some(Self {
duration,
timer_registry_id,
}),
None => None,
}
}
const fn assert_instances_are_compatible(&self, other: &Self) {
if self.timer_registry_id != other.timer_registry_id {
panic!("Operations between Instant's from different TimerRegistry instances are not supported.");
}
}
}
impl Add<Duration> for Instant {
type Output = Instant;
fn add(self, rhs: Duration) -> Self::Output {
Self {
duration: self.duration.add(rhs),
timer_registry_id: self.timer_registry_id,
}
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, rhs: Duration) {
self.duration.add_assign(rhs);
}
}
impl Sub<Duration> for Instant {
type Output = Instant;
fn sub(self, rhs: Duration) -> Self::Output {
Self {
duration: self.duration.sub(rhs),
timer_registry_id: self.timer_registry_id,
}
}
}
impl Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, rhs: Instant) -> Self::Output {
self.assert_instances_are_compatible(&rhs);
self.duration.sub(rhs.duration)
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, rhs: Duration) {
self.duration.sub_assign(rhs);
}
}
#[cfg(test)]
mod test {
use super::*;
use std::time::Duration;
#[test]
#[should_panic]
fn should_not_allow_fetching_duration_from_incorrect_timer_registry() {
Instant::new(Duration::ZERO, 0).into_duration(1);
}
}