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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use core::{fmt, ops};
use crate::utils::{Init, Zeroable};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Zeroable)]
#[repr(transparent)]
pub struct Duration {
micros: i32,
}
impl Init for Duration {
const INIT: Self = Self::ZERO;
}
impl Default for Duration {
fn default() -> Self {
Self::INIT
}
}
impl Duration {
pub const ZERO: Self = Duration { micros: 0 };
pub const MAX: Self = Duration { micros: i32::MAX };
pub const MIN: Self = Duration { micros: i32::MIN };
#[inline]
pub const fn from_micros(micros: i32) -> Self {
Self { micros }
}
#[inline]
pub const fn from_millis(millis: i32) -> Self {
Self::from_micros(millis.checked_mul(1_000).expect("duration overflow"))
}
#[inline]
pub const fn from_secs(secs: i32) -> Self {
Self::from_micros(secs.checked_mul(1_000_000).expect("duration overflow"))
}
#[inline]
pub const fn as_micros(self) -> i32 {
self.micros
}
#[inline]
pub const fn as_millis(self) -> i32 {
self.micros / 1_000
}
#[inline]
pub const fn as_secs(self) -> i32 {
self.micros / 1_000_000
}
#[inline]
pub const fn as_secs_f64(self) -> f64 {
self.micros as f64 / 1_000_000.0
}
#[inline]
pub const fn as_secs_f32(self) -> f32 {
(self.micros / 1_000_000) as f32 + (self.micros % 1_000_000) as f32 / 1_000_000.0
}
#[inline]
pub const fn is_positive(self) -> bool {
self.micros.is_positive()
}
#[inline]
pub const fn is_negative(self) -> bool {
self.micros.is_negative()
}
#[inline]
pub const fn checked_mul(self, other: i32) -> Option<Self> {
self.micros.checked_mul(other).map(Self::from_micros)
}
#[inline]
pub const fn checked_div(self, other: i32) -> Option<Self> {
self.micros.checked_div(other).map(Self::from_micros)
}
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
self.micros.checked_abs().map(Self::from_micros)
}
#[inline]
pub const fn checked_add(self, other: Self) -> Option<Self> {
self.micros.checked_add(other.micros).map(Self::from_micros)
}
#[inline]
pub const fn checked_sub(self, other: Self) -> Option<Self> {
self.micros.checked_sub(other.micros).map(Self::from_micros)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TryFromDurationError(());
impl TryFrom<core::time::Duration> for Duration {
type Error = TryFromDurationError;
fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
Ok(Self::from_micros(
value
.as_micros()
.try_into()
.map_err(|_| TryFromDurationError(()))?,
))
}
}
impl TryFrom<Duration> for core::time::Duration {
type Error = TryFromDurationError;
fn try_from(value: Duration) -> Result<Self, Self::Error> {
if value.micros < 0 {
Err(TryFromDurationError(()))
} else {
Ok(Self::from_micros(value.micros as u64))
}
}
}
impl fmt::Debug for Duration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let abs_dur = core::time::Duration::from_micros(self.micros.unsigned_abs().into());
if self.micros < 0 {
write!(f, "-")?;
}
abs_dur.fmt(f)
}
}
impl ops::Add for Duration {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
self.checked_add(rhs)
.expect("overflow when adding durations")
}
}
impl ops::AddAssign for Duration {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl ops::Sub for Duration {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
self.checked_sub(rhs)
.expect("overflow when subtracting durations")
}
}
impl ops::SubAssign for Duration {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl ops::Mul<i32> for Duration {
type Output = Duration;
#[inline]
fn mul(self, rhs: i32) -> Self::Output {
self.checked_mul(rhs)
.expect("overflow when multiplying duration by scalar")
}
}
impl ops::Mul<Duration> for i32 {
type Output = Duration;
#[inline]
fn mul(self, rhs: Duration) -> Self::Output {
rhs.checked_mul(self)
.expect("overflow when multiplying duration by scalar")
}
}
impl ops::MulAssign<i32> for Duration {
#[inline]
fn mul_assign(&mut self, rhs: i32) {
*self = *self * rhs;
}
}
impl ops::Div<i32> for Duration {
type Output = Duration;
#[inline]
fn div(self, rhs: i32) -> Self::Output {
self.checked_div(rhs)
.expect("divide by zero or overflow when dividing duration by scalar")
}
}
impl ops::DivAssign<i32> for Duration {
#[inline]
fn div_assign(&mut self, rhs: i32) {
*self = *self / rhs;
}
}
impl core::iter::Sum for Duration {
fn sum<I: Iterator<Item = Duration>>(iter: I) -> Self {
iter.fold(Duration::ZERO, |x, y| {
x.checked_add(y)
.expect("overflow in iter::sum over durations")
})
}
}
impl<'a> core::iter::Sum<&'a Duration> for Duration {
fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Self {
iter.cloned().sum()
}
}
#[cfg(feature = "chrono_0p4")]
impl TryFrom<chrono_0p4::Duration> for Duration {
type Error = TryFromDurationError;
fn try_from(value: chrono_0p4::Duration) -> Result<Self, Self::Error> {
Ok(Self::from_micros(
value
.num_microseconds()
.and_then(|x| x.try_into().ok())
.ok_or(TryFromDurationError(()))?,
))
}
}
#[cfg(feature = "chrono_0p4")]
impl From<Duration> for chrono_0p4::Duration {
fn from(value: Duration) -> Self {
Self::microseconds(value.micros as i64)
}
}