veecle-osal-api 0.1.0

Veecle OS operating system abstraction layer API
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! This module implements a [`Duration`] with microsecond precision.

use core::fmt;
use core::num::TryFromIntError;
use core::ops::{Add, Div, Mul, Sub};

/// Duration represents a span of time.
///
/// Negative durations are not supported. [`Duration`] is not meant to be used
/// for math operations.
#[derive(
    Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub struct Duration {
    micros: u64,
}

impl Duration {
    /// The largest value that can be represented by the `Duration` type.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::MAX, Duration::from_micros(u64::MAX));
    /// ```
    pub const MAX: Duration = Duration { micros: u64::MAX };

    /// A duration of zero time.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::ZERO, Duration::from_micros(0));
    /// ```
    pub const ZERO: Duration = Duration { micros: 0 };

    /// Factor of microseconds per second.
    const MICROS_PER_SECOND: u64 = 1_000_000;
    /// Factor of milliseconds per second.
    const MILLIS_PER_SECOND: u64 = 1_000;

    /// Creates a duration from the specified number of seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1), Duration::from_millis(1000));
    /// ```
    pub const fn from_secs(secs: u64) -> Duration {
        Duration {
            micros: secs * Self::MICROS_PER_SECOND,
        }
    }

    /// Creates a duration from the specified number of milliseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1), Duration::from_millis(1000));
    /// ```
    pub const fn from_millis(millis: u64) -> Duration {
        Duration {
            micros: millis * Self::MILLIS_PER_SECOND,
        }
    }

    /// Creates a duration from the specified number of microseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1), Duration::from_micros(1000000));
    /// ```
    pub const fn from_micros(micros: u64) -> Duration {
        Duration { micros }
    }

    /// Returns the total amount of seconds, rounded down.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_millis(1980).as_secs(), 1);
    /// ```
    pub const fn as_secs(&self) -> u64 {
        self.micros / Self::MICROS_PER_SECOND
    }

    /// Returns the total amount of milliseconds, rounded down.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_millis(1980).as_millis(), 1980);
    /// ```
    pub const fn as_millis(&self) -> u64 {
        self.micros / Self::MILLIS_PER_SECOND
    }

    /// Returns the total amount of microseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_millis(1980).as_micros(), 1980000);
    /// ```
    pub const fn as_micros(&self) -> u64 {
        self.micros
    }

    /// Adds one Duration to another, returning a new Duration or None in the event of an overflow.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(
    ///     Duration::from_secs(1).checked_add(Duration::from_secs(1)),
    ///     Some(Duration::from_secs(2))
    /// );
    /// assert_eq!(Duration::MAX.checked_add(Duration::from_secs(1)), None);
    /// ```
    pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
        self.micros
            .checked_add(rhs.micros)
            .map(|micros| Duration { micros })
    }

    /// Subtracts one Duration from another, returning a new Duration or None in the event of an underflow.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(
    ///     Duration::from_secs(2).checked_sub(Duration::from_secs(1)),
    ///     Some(Duration::from_secs(1))
    /// );
    /// assert_eq!(Duration::from_secs(1).checked_sub(Duration::from_secs(2)), None);
    /// ```
    pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
        self.micros
            .checked_sub(rhs.micros)
            .map(|micros| Duration { micros })
    }

    /// Multiplies one Duration by a scalar `u32`, returning a new Duration or None in the event of an overflow.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1).checked_mul(2), Some(Duration::from_secs(2)));
    /// assert_eq!(Duration::MAX.checked_mul(2), None);
    /// ```
    pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
        self.micros
            .checked_mul(rhs as _)
            .map(|micros| Duration { micros })
    }

    /// Divides one Duration by a scalar `u32`, returning a new Duration or None if `rhs == 0`.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1).checked_div(2), Some(Duration::from_millis(500)));
    /// assert_eq!(Duration::from_secs(1).checked_div(0), None);
    /// ```
    pub fn checked_div(self, rhs: u32) -> Option<Duration> {
        self.micros
            .checked_div(rhs as _)
            .map(|micros| Duration { micros })
    }

    /// Returns the absolute difference between self and rhs.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1).abs_diff(Duration::from_secs(2)), Duration::from_secs(1));
    /// assert_eq!(Duration::from_secs(2).abs_diff(Duration::from_secs(1)), Duration::from_secs(1));
    /// ```
    pub fn abs_diff(self, rhs: Self) -> Duration {
        Self {
            micros: self.micros.abs_diff(rhs.micros),
        }
    }

    /// Returns a duration of approximately 50 years.
    ///
    /// No leap-seconds/days have been taken into account.
    ///
    /// Can be used in place of [MAX][Self::MAX] to avoid overflows.
    pub(crate) fn max_no_overflow_alias() -> Self {
        // seconds * minutes * hours * days * weeks * years
        Self::from_secs(60 * 60 * 24 * 7 * 52 * 50)
    }
}

impl Add for Duration {
    type Output = Self;

    /// # Panics
    ///
    /// This function may panic if the resulting duration overflows. See [`Duration::checked_add`] for a version
    /// without panic.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1) + Duration::from_secs(1), Duration::from_secs(2));
    /// ```
    ///
    /// ```should_panic
    /// use veecle_osal_api::time::Duration;
    ///
    /// let _ = Duration::MAX + Duration::from_secs(1);
    /// ```
    fn add(self, rhs: Self) -> Self::Output {
        let Some(result) = self.checked_add(rhs) else {
            panic!("overflow when adding two durations");
        };

        result
    }
}

impl Sub for Duration {
    type Output = Self;

    /// # Panics
    ///
    /// This function may panic if the resulting duration underflows. See [`Duration::checked_sub`] for a
    /// version without panic.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(2) - Duration::from_secs(1), Duration::from_secs(1));
    /// ```
    ///
    /// ```should_panic
    /// use veecle_osal_api::time::Duration;
    ///
    /// let _ = Duration::from_secs(1) - Duration::from_secs(2);
    /// ```
    fn sub(self, rhs: Self) -> Self::Output {
        let Some(result) = self.checked_sub(rhs) else {
            panic!("underflow when subtracting two durations");
        };

        result
    }
}

impl Mul<u32> for Duration {
    type Output = Self;

    /// # Panics
    ///
    /// This function may panic if the resulting duration overflows. See [`Duration::checked_mul`] for a version
    /// without panic.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1) * 2, Duration::from_secs(2));
    /// ```
    ///
    /// ```should_panic
    /// use veecle_osal_api::time::Duration;
    ///
    /// let _ = Duration::MAX * 2;
    /// ```
    fn mul(self, rhs: u32) -> Self::Output {
        let Some(result) = self.checked_mul(rhs) else {
            panic!("overflow when multiplying a duration by a scalar");
        };

        result
    }
}

impl Mul<Duration> for u32 {
    type Output = Duration;

    /// # Panics
    ///
    /// This function may panic if the resulting duration overflows. See [`Duration::checked_mul`] for a version
    /// without panic.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(2 * Duration::from_secs(1), Duration::from_secs(2));
    /// ```
    ///
    /// ```should_panic
    /// use veecle_osal_api::time::Duration;
    ///
    /// let _ = 2 * Duration::MAX;
    /// ```
    fn mul(self, rhs: Duration) -> Self::Output {
        rhs * self
    }
}

impl Div<u32> for Duration {
    type Output = Self;

    /// # Panics
    ///
    /// This function may panic if the duration is divided by zero. See [`Duration::checked_div`] for a version
    /// without panic.
    ///
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::from_secs(1) / 2, Duration::from_millis(500));
    /// ```
    ///
    /// ```should_panic
    /// use veecle_osal_api::time::Duration;
    ///
    /// let _ = Duration::from_secs(1) / 0;
    /// ```
    fn div(self, rhs: u32) -> Self::Output {
        let Some(result) = self.checked_div(rhs) else {
            panic!("divided a duration by zero");
        };

        result
    }
}

impl fmt::Debug for Duration {
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// let duration = Duration::from_millis(1980);
    /// assert_eq!(format!("{duration:?}"), "1s.980000us");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}s.{}us",
            self.as_secs(),
            self.as_micros() % Self::MICROS_PER_SECOND
        )
    }
}

impl TryFrom<core::time::Duration> for Duration {
    type Error = TryFromIntError;

    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(Duration::try_from(core::time::Duration::from_secs(1)), Ok(Duration::from_secs(1)));
    /// ```
    fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
        value.as_micros().try_into().map(Self::from_micros)
    }
}

impl From<Duration> for core::time::Duration {
    /// # Examples
    ///
    /// ```
    /// use veecle_osal_api::time::Duration;
    ///
    /// assert_eq!(
    ///     core::time::Duration::from(Duration::from_secs(1)),
    ///     core::time::Duration::from_secs(1)
    /// );
    /// ```
    fn from(value: Duration) -> Self {
        Self::from_micros(value.as_micros())
    }
}