rkyv/
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 = "strict", repr(C))]
8pub struct ArchivedDuration {
9    secs: Archived<u64>,
10    nanos: Archived<u32>,
11}
12
13const NANOS_PER_SEC: u32 = 1_000_000_000;
14const NANOS_PER_MILLI: u32 = 1_000_000;
15const NANOS_PER_MICRO: u32 = 1_000;
16const MILLIS_PER_SEC: u64 = 1_000;
17const MICROS_PER_SEC: u64 = 1_000_000;
18
19impl ArchivedDuration {
20    /// Returns the number of _whole_ seconds contained by this
21    /// `ArchivedDuration`.
22    ///
23    /// The returned value does not include the fractional (nanosecond) part of the duration, which
24    /// can be obtained using [`subsec_nanos`].
25    ///
26    /// [`subsec_nanos`]: ArchivedDuration::subsec_nanos
27    #[inline]
28    pub const fn as_secs(&self) -> u64 {
29        from_archived!(self.secs)
30    }
31
32    /// Returns the fractional part of this `ArchivedDuration`, in whole milliseconds.
33    ///
34    /// This method does **not** return the length of the duration when represented by milliseconds.
35    /// The returned number always represents a fractional portion of a second (i.e., it is less
36    /// than one thousand).
37    #[inline]
38    pub const fn subsec_millis(&self) -> u32 {
39        from_archived!(self.nanos) / NANOS_PER_MILLI
40    }
41
42    /// Returns the fractional part of this `ArchivedDuration`, in whole microseconds.
43    ///
44    /// This method does **not** return the length of the duration when represented by microseconds.
45    /// The returned number always represents a fractional portion of a second (i.e., it is less
46    /// than one million).
47    #[inline]
48    pub const fn subsec_micros(&self) -> u32 {
49        from_archived!(self.nanos) / NANOS_PER_MICRO
50    }
51
52    /// Returns the fractional part of this `Duration`, in nanoseconds.
53    ///
54    /// This method does **not** return the length of the duration when represented by nanoseconds.
55    /// The returned number always represents a fractional portion of a second (i.e., it is less
56    /// than one billion).
57    #[inline]
58    pub const fn subsec_nanos(&self) -> u32 {
59        from_archived!(self.nanos)
60    }
61
62    /// Returns the total number of whole milliseconds contained by this `ArchivedDuration`.
63    #[inline]
64    pub const fn as_millis(&self) -> u128 {
65        self.as_secs() as u128 * MILLIS_PER_SEC as u128
66            + (self.subsec_nanos() / NANOS_PER_MILLI) as u128
67    }
68
69    /// Returns the total number of whole microseconds contained by this `ArchivedDuration`.
70    #[inline]
71    pub const fn as_micros(&self) -> u128 {
72        self.as_secs() as u128 * MICROS_PER_SEC as u128
73            + (self.subsec_nanos() / NANOS_PER_MICRO) as u128
74    }
75
76    /// Returns the total number of nanoseconds contained by this `ArchivedDuration`.
77    #[inline]
78    pub const fn as_nanos(&self) -> u128 {
79        self.as_secs() as u128 * NANOS_PER_SEC as u128 + self.subsec_nanos() as u128
80    }
81
82    /// Returns the number of seconds contained by this `ArchivedDuration` as `f64`.
83    ///
84    /// The returned value does include the fractional (nanosecond) part of the duration.
85    #[inline]
86    pub fn as_secs_f64(&self) -> f64 {
87        (self.as_secs() as f64) + (self.subsec_nanos() as f64) / (NANOS_PER_SEC as f64)
88    }
89
90    /// Returns the number of seconds contained by this `ArchivedDuration` as `f32`.
91    ///
92    /// The returned value does include the fractional (nanosecond) part of the duration.
93    #[inline]
94    pub fn as_secs_f32(&self) -> f32 {
95        (self.as_secs() as f32) + (self.subsec_nanos() as f32) / (NANOS_PER_SEC as f32)
96    }
97
98    /// Constructs an archived duration at the given position.
99    ///
100    /// # Safety
101    ///
102    /// `out` must point to memory suitable for holding an `ArchivedDuration`.
103    #[inline]
104    pub unsafe fn emplace(secs: u64, nanos: u32, out: *mut ArchivedDuration) {
105        use core::ptr::addr_of_mut;
106
107        addr_of_mut!((*out).secs).write(to_archived!(secs));
108        addr_of_mut!((*out).nanos).write(to_archived!(nanos));
109    }
110}
111
112#[cfg(feature = "validation")]
113const _: () = {
114    use crate::Fallible;
115    use bytecheck::CheckBytes;
116    use core::fmt;
117
118    /// An error resulting from an invalid duration.
119    ///
120    /// Durations must have a `secs` and `nanos` that when combined do not overflow a `u64`.
121    #[derive(Debug)]
122    pub struct DurationError;
123
124    impl fmt::Display for DurationError {
125        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126            write!(f, "Duration error: nanos field is greater than 1 billion and overflows the seconds counter")
127        }
128    }
129
130    #[cfg(feature = "std")]
131    impl std::error::Error for DurationError {}
132
133    impl<C: Fallible + ?Sized> CheckBytes<C> for ArchivedDuration {
134        type Error = DurationError;
135
136        #[inline]
137        unsafe fn check_bytes<'a>(value: *const Self, _: &mut C) -> Result<&'a Self, Self::Error> {
138            // The fields of `ArchivedDuration` are always valid
139            let duration = &*value;
140            let secs = from_archived!(duration.secs);
141
142            if secs
143                .checked_add((duration.nanos / 1_000_000_000) as u64)
144                .is_none()
145            {
146                Err(DurationError)
147            } else {
148                Ok(duration)
149            }
150        }
151    }
152};