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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![deny(warnings)]
use std::cmp::Ordering;
use std::fmt;
use thiserror::Error;
mod source;
pub use crate::source::*;
#[derive(Debug, Error)]
pub enum Error {
#[error("Offset greater than limit")]
OffsetTooGreat,
#[error("Outside of specified offset")]
SystemTime(#[from] std::time::SystemTimeError),
#[error("Integer conversion error")]
FromInt(#[from] std::num::TryFromIntError),
#[error("Outside supported time range: {0}ticks")]
SupportedTime(u128),
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp<T> {
pub time: T,
pub count: u16,
}
#[derive(Debug, Clone)]
pub struct Clock<S: ClockSource> {
src: S,
last_observed: Timestamp<S::Time>,
}
#[derive(Debug, Clone)]
pub struct OffsetLimiter<S: ClockSource> {
clock: Clock<S>,
max_offset: S::Delta,
}
impl Clock<WallMS> {
pub fn wall_ms() -> Result<Clock<WallMS>> {
Clock::new(WallMS)
}
}
impl Clock<ManualClock> {
pub fn manual(t: u64) -> Result<Clock<ManualClock>> {
Clock::new(ManualClock::new(t))
}
pub fn set_time(&mut self, t: u64) {
self.src.set_time(t)
}
}
impl<S: ClockSource> Clock<S> {
pub fn new(mut src: S) -> Result<Self> {
let init = src.now()?;
let clock = Clock {
src,
last_observed: Timestamp {
time: init,
count: 0,
},
};
Ok(clock)
}
pub fn with_max_diff(self, max_offset: S::Delta) -> OffsetLimiter<S> {
OffsetLimiter::new(self, max_offset)
}
pub fn now(&mut self) -> Result<Timestamp<S::Time>> {
let pt = self.read_pt()?;
self.do_observe(&pt);
Ok(self.last_observed)
}
fn do_observe(&mut self, observation: &Timestamp<S::Time>) {
let lp = self.last_observed.clone();
self.last_observed = match (
lp.time.cmp(&observation.time),
lp.count.cmp(&observation.count),
) {
(Ordering::Less, _) => observation.clone(),
(Ordering::Equal, Ordering::Less) => Timestamp {
count: observation.count + 1,
..lp
},
_ => Timestamp {
count: lp.count + 1,
..lp
},
};
}
pub fn observe(&mut self, msg: &Timestamp<S::Time>) {
self.do_observe(&msg);
}
fn read_pt(&mut self) -> Result<Timestamp<S::Time>> {
Ok(Timestamp {
time: self.src.now()?,
count: 0,
})
}
}
impl<S: ClockSource> OffsetLimiter<S> {
pub fn new(clock: Clock<S>, max_offset: S::Delta) -> Self {
OffsetLimiter { clock, max_offset }
}
pub fn observe(&mut self, msg: &Timestamp<S::Time>) -> Result<()> {
let pt = self.clock.read_pt()?;
self.verify_offset(&pt, msg)?;
self.clock.observe(&msg);
Ok(())
}
pub fn now(&mut self) -> Result<Timestamp<S::Time>> {
self.clock.now()
}
fn verify_offset(&self, pt: &Timestamp<S::Time>, msg: &Timestamp<S::Time>) -> Result<()> {
if msg.time <= pt.time {
return Ok(());
}
let diff = msg.time - pt.time;
if diff > self.max_offset {
return Err(Error::OffsetTooGreat);
}
Ok(())
}
pub fn into_inner(self) -> Clock<S> {
self.clock
}
pub fn inner(&self) -> &Clock<S> {
&self.clock
}
pub fn inner_mut(&mut self) -> &mut Clock<S> {
&mut self.clock
}
}
impl<T: fmt::Display> fmt::Display for Timestamp<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{}+{}", self.time, self.count)
}
}
impl<T> Timestamp<T> {
pub fn time_into<U: From<T>>(self) -> Timestamp<U> {
Timestamp {
time: self.time.into(),
count: self.count,
}
}
}
#[cfg(test)]
mod tests {
use super::Timestamp;
use suppositions::generators::*;
pub fn timestamps<C: Generator + 'static>(
times: C,
) -> Box<dyn GeneratorObject<Item = Timestamp<C::Item>>> {
let counts = u16s();
(times, counts)
.map(|(time, count)| Timestamp { time, count })
.boxed()
}
}