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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::collections::BTreeSet;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::{Duration, SystemTime};
#[derive(Wrapper, WrapperMut, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, From)]
#[wrapper(Display, LowerHex, UpperHex, Octal, Add, Sub)]
#[wrapper_mut(AddAssign, SubAssign)]
pub struct Timestamp(u128);
impl Timestamp {
pub fn now() -> Self {
let duration =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("system time");
Self(duration.as_millis())
}
pub fn from_secs(secs: u64) -> Timestamp { Timestamp(secs as u128 * 1000) }
pub fn from_millis(millis: u128) -> Timestamp { Timestamp(millis) }
#[deprecated(note = "use Timestamp::as_secs")]
pub fn into_secs(self) -> u64 { self.as_secs() }
pub fn as_secs(&self) -> u64 { (self.0 / 1000) as u64 }
pub fn as_millis(&self) -> u64 {
self.0 as u64
}
}
impl Add<Duration> for Timestamp {
type Output = Timestamp;
fn add(self, rhs: Duration) -> Self::Output { Timestamp(self.0 + rhs.as_millis()) }
}
impl Sub<Duration> for Timestamp {
type Output = Timestamp;
fn sub(self, rhs: Duration) -> Self::Output { Timestamp(self.0 - rhs.as_millis()) }
}
impl AddAssign<Duration> for Timestamp {
fn add_assign(&mut self, rhs: Duration) { self.0 += rhs.as_millis() }
}
impl SubAssign<Duration> for Timestamp {
fn sub_assign(&mut self, rhs: Duration) { self.0 -= rhs.as_millis() }
}
#[derive(Debug, Default)]
pub struct Timer {
timeouts: BTreeSet<Timestamp>,
}
impl Timer {
pub fn new() -> Self { Self { timeouts: bset! {} } }
pub fn len(&self) -> usize { self.timeouts.len() }
pub fn is_empty(&self) -> bool { self.timeouts.is_empty() }
pub fn set_timer(&mut self, span: Duration, after: Timestamp) {
let time = after + Timestamp(span.as_millis());
self.timeouts.insert(time);
}
pub fn next(&self, after: impl Into<Timestamp>) -> Option<Duration> {
let after = after.into();
self.timeouts
.iter()
.find(|t| **t >= after)
.map(|t| Duration::from_millis((*t - after).as_millis()))
}
pub fn expire(&mut self, time: Timestamp) -> usize {
let at = time + Timestamp::from_millis(1);
let unexpired = self.timeouts.split_off(&at);
let fired = self.timeouts.len();
self.timeouts = unexpired;
fired
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wake_exact() {
let mut tm = Timer::new();
let now = Timestamp::now();
tm.set_timer(Duration::from_secs(8), now);
tm.set_timer(Duration::from_secs(9), now);
tm.set_timer(Duration::from_secs(10), now);
assert_eq!(tm.expire(now + Duration::from_secs(9)), 2);
assert_eq!(tm.len(), 1);
}
#[test]
fn test_wake() {
let mut tm = Timer::new();
let now = Timestamp::now();
tm.set_timer(Duration::from_secs(8), now);
tm.set_timer(Duration::from_secs(16), now);
tm.set_timer(Duration::from_secs(64), now);
tm.set_timer(Duration::from_secs(72), now);
assert_eq!(tm.expire(now), 0);
assert_eq!(tm.len(), 4);
assert_eq!(tm.expire(now + Duration::from_secs(9)), 1);
assert_eq!(tm.len(), 3, "one timeout has expired");
assert_eq!(tm.expire(now + Duration::from_secs(66)), 2);
assert_eq!(tm.len(), 1, "another two timeouts have expired");
assert_eq!(tm.expire(now + Duration::from_secs(96)), 1);
assert!(tm.is_empty(), "all timeouts have expired");
}
}