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
221
222
223
224
225
226
227
228
229
230
use derive_more::{
Add, AddAssign, Binary, Deref, Display, Into, LowerHex, MulAssign, Octal, Sum, UpperHex,
};
use std::ops;
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Display,
Deref,
Into,
Binary,
Octal,
LowerHex,
UpperHex,
Add,
Sum,
AddAssign,
MulAssign,
)]
#[display(fmt = "{_0}")]
pub struct Frequency(pub(crate) u32);
impl Frequency {
pub fn is_unitless(&self) -> bool {
self.0 == 0
}
}
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Display,
Binary,
Octal,
LowerHex,
UpperHex,
Add,
Sum,
AddAssign,
MulAssign,
)]
#[display(fmt = "{_0}")]
pub struct Timestamp(pub(crate) u64);
impl Timestamp {
pub fn zero() -> Self {
Self(0)
}
pub fn ticks(&self) -> u64 {
self.0
}
}
impl ops::Add<DifferentialTimestamp> for Timestamp {
type Output = Timestamp;
fn add(self, dt: DifferentialTimestamp) -> Timestamp {
Timestamp(
self.0
.checked_add(u64::from(dt.0))
.expect("Overflow when adding differential time to timestamp"),
)
}
}
impl ops::AddAssign<DifferentialTimestamp> for Timestamp {
fn add_assign(&mut self, dt: DifferentialTimestamp) {
self.0 = self
.0
.checked_add(u64::from(dt.0))
.expect("Overflow when adding differential time to timestamp")
}
}
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Display,
Binary,
Octal,
LowerHex,
UpperHex,
Add,
Sum,
AddAssign,
MulAssign,
)]
#[display(fmt = "{_0}")]
pub struct DifferentialTimestamp(pub(crate) u32);
impl DifferentialTimestamp {
pub fn ticks(&self) -> u32 {
self.0
}
}
impl DifferentialTimestamp {
pub(crate) fn from_xts8(xts_8: u8, xts_16: u16) -> Self {
DifferentialTimestamp(u32::from(xts_8) << 24 | (u32::from(xts_16) << 8))
}
pub(crate) fn from_xts16(xts_16: u16) -> Self {
DifferentialTimestamp(u32::from(xts_16) << 16)
}
pub fn zero() -> Self {
Self(0)
}
pub fn clear(&mut self) {
self.0 = 0;
}
}
impl ops::AddAssign<Dts8> for DifferentialTimestamp {
fn add_assign(&mut self, dts: Dts8) {
self.0 = self
.0
.checked_add(u32::from(dts.0))
.expect("Overflow when adding DTS8 to differential time")
}
}
impl ops::AddAssign<Dts16> for DifferentialTimestamp {
fn add_assign(&mut self, dts: Dts16) {
self.0 = self
.0
.checked_add(u32::from(dts.0))
.expect("Overflow when adding DTS16 to differential time")
}
}
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Into,
Display,
Binary,
Octal,
LowerHex,
UpperHex,
)]
#[display(fmt = "{_0}")]
pub struct Dts8(pub(crate) u8);
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Into,
Display,
Binary,
Octal,
LowerHex,
UpperHex,
)]
#[display(fmt = "{_0}")]
pub struct Dts16(pub(crate) u16);
#[cfg(test)]
mod test {
use super::*;
#[test]
fn differential_time_xts16() {
let mut accumulated_time = Timestamp::zero();
accumulated_time.0 += 0x0F;
assert_eq!(accumulated_time.ticks(), 0x0F);
let xts_16 = 0x00_03;
let mut dts_for_next_event = DifferentialTimestamp::from_xts16(xts_16);
assert_eq!(dts_for_next_event.ticks(), 0x00_03_00_00);
let dts = Dts16(0x5F_D5);
dts_for_next_event += dts;
assert_eq!(dts_for_next_event.ticks(), 0x00_03_5F_D5);
accumulated_time += dts_for_next_event;
assert_eq!(accumulated_time.ticks(), 0x00_03_5F_D5 + 0x0F);
}
}