rkyv_test/time.rs
1//! Archived versions of `time` types.
2
3use crate::Archived;
4
5/// An archived [`Duration`](core::time::Duration).
6#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
7#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
8#[cfg_attr(feature = "strict", repr(C))]
9pub struct ArchivedDuration {
10 secs: Archived<u64>,
11 nanos: Archived<u32>,
12}
13
14const NANOS_PER_SEC: u32 = 1_000_000_000;
15const NANOS_PER_MILLI: u32 = 1_000_000;
16const NANOS_PER_MICRO: u32 = 1_000;
17const MILLIS_PER_SEC: u64 = 1_000;
18const MICROS_PER_SEC: u64 = 1_000_000;
19
20impl ArchivedDuration {
21 /// Returns the number of _whole_ seconds contained by this
22 /// `ArchivedDuration`.
23 ///
24 /// The returned value does not include the fractional (nanosecond) part of the duration, which
25 /// can be obtained using [`subsec_nanos`].
26 ///
27 /// [`subsec_nanos`]: ArchivedDuration::subsec_nanos
28 #[inline]
29 pub const fn as_secs(&self) -> u64 {
30 from_archived!(self.secs)
31 }
32
33 /// Returns the fractional part of this `ArchivedDuration`, in whole milliseconds.
34 ///
35 /// This method does **not** return the length of the duration when represented by milliseconds.
36 /// The returned number always represents a fractional portion of a second (i.e., it is less
37 /// than one thousand).
38 #[inline]
39 pub const fn subsec_millis(&self) -> u32 {
40 from_archived!(self.nanos) / NANOS_PER_MILLI
41 }
42
43 /// Returns the fractional part of this `ArchivedDuration`, in whole microseconds.
44 ///
45 /// This method does **not** return the length of the duration when represented by microseconds.
46 /// The returned number always represents a fractional portion of a second (i.e., it is less
47 /// than one million).
48 #[inline]
49 pub const fn subsec_micros(&self) -> u32 {
50 from_archived!(self.nanos) / NANOS_PER_MICRO
51 }
52
53 /// Returns the fractional part of this `Duration`, in nanoseconds.
54 ///
55 /// This method does **not** return the length of the duration when represented by nanoseconds.
56 /// The returned number always represents a fractional portion of a second (i.e., it is less
57 /// than one billion).
58 #[inline]
59 pub const fn subsec_nanos(&self) -> u32 {
60 from_archived!(self.nanos)
61 }
62
63 /// Returns the total number of whole milliseconds contained by this `ArchivedDuration`.
64 #[inline]
65 pub const fn as_millis(&self) -> u128 {
66 self.as_secs() as u128 * MILLIS_PER_SEC as u128
67 + (self.subsec_nanos() / NANOS_PER_MILLI) as u128
68 }
69
70 /// Returns the total number of whole microseconds contained by this `ArchivedDuration`.
71 #[inline]
72 pub const fn as_micros(&self) -> u128 {
73 self.as_secs() as u128 * MICROS_PER_SEC as u128
74 + (self.subsec_nanos() / NANOS_PER_MICRO) as u128
75 }
76
77 /// Returns the total number of nanoseconds contained by this `ArchivedDuration`.
78 #[inline]
79 pub const fn as_nanos(&self) -> u128 {
80 self.as_secs() as u128 * NANOS_PER_SEC as u128 + self.subsec_nanos() as u128
81 }
82
83 /// Returns the number of seconds contained by this `ArchivedDuration` as `f64`.
84 ///
85 /// The returned value does include the fractional (nanosecond) part of the duration.
86 #[inline]
87 pub fn as_secs_f64(&self) -> f64 {
88 (self.as_secs() as f64) + (self.subsec_nanos() as f64) / (NANOS_PER_SEC as f64)
89 }
90
91 /// Returns the number of seconds contained by this `ArchivedDuration` as `f32`.
92 ///
93 /// The returned value does include the fractional (nanosecond) part of the duration.
94 #[inline]
95 pub fn as_secs_f32(&self) -> f32 {
96 (self.as_secs() as f32) + (self.subsec_nanos() as f32) / (NANOS_PER_SEC as f32)
97 }
98
99 /// Constructs an archived duration at the given position.
100 ///
101 /// # Safety
102 ///
103 /// `out` must point to memory suitable for holding an `ArchivedDuration`.
104 #[inline]
105 pub unsafe fn emplace(secs: u64, nanos: u32, out: *mut ArchivedDuration) {
106 use core::ptr::addr_of_mut;
107
108 addr_of_mut!((*out).secs).write(to_archived!(secs));
109 addr_of_mut!((*out).nanos).write(to_archived!(nanos));
110 }
111}