packet_formats/
utils.rs

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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Utilities useful when parsing and serializing wire formats.

use core::num::{NonZeroU32, NonZeroU64};
use core::time::Duration;

/// A thin wrapper over a [`Duration`] that guarantees that the underlying
/// `Duration` is non-zero.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub struct NonZeroDuration(Duration);

impl NonZeroDuration {
    /// Creates a non-zero without checking the value.
    ///
    /// # Safety
    ///
    /// If `d` is zero, unsafe code which relies on the invariant that
    /// `NonZeroDuration` values are always zero may cause undefined behavior.
    pub const unsafe fn new_unchecked(d: Duration) -> NonZeroDuration {
        NonZeroDuration(d)
    }

    /// Creates a new `NonZeroDuration` from the specified number of whole
    /// seconds if that number is non-zero.
    pub const fn from_secs(secs: u64) -> Option<NonZeroDuration> {
        NonZeroDuration::new(Duration::from_secs(secs))
    }

    /// Creates a new `NonZeroDuration` from the specified non-zero number of
    /// whole seconds.
    pub const fn from_nonzero_secs(secs: NonZeroU64) -> NonZeroDuration {
        NonZeroDuration(Duration::from_secs(secs.get()))
    }

    /// Creates a new `NonZeroDuration` from the specified number of whole
    /// seconds and additional nanoseconds if the resulting duration is
    /// non-zero.
    ///
    /// If the number of nanoseconds is greater than 1 billion (the number of
    /// nanoseconds in a second), then it will carry over into the seconds
    /// provided.
    ///
    /// # Panics
    ///
    /// This constructor will panic if the carry from the nanoseconds overflows
    /// the seconds counter.
    pub const fn from_secs_nanos(secs: u64, nanos: u32) -> Option<NonZeroDuration> {
        NonZeroDuration::new(Duration::new(secs, nanos))
    }

    /// Creates a new `NonZeroDuration` from the specified non-zero number of
    /// whole seconds and additional nanoseconds.
    ///
    /// If the number of nanoseconds is greater than 1 billion (the number of
    /// nanoseconds in a second), then it will carry over into the seconds
    /// provided.
    ///
    /// # Panics
    ///
    /// This constructor will panic if the carry from the nanoseconds overflows
    /// the seconds counter.
    pub const fn from_nonzero_secs_nanos(secs: NonZeroU64, nanos: NonZeroU32) -> NonZeroDuration {
        NonZeroDuration(Duration::new(secs.get(), nanos.get()))
    }

    /// Creates a new `NonZeroDuration` from the specified number of
    /// milliseconds if that number is non-zero.
    pub const fn from_millis(millis: u64) -> Option<NonZeroDuration> {
        NonZeroDuration::new(Duration::from_millis(millis))
    }

    /// Creates a new `NonZeroDuration` from the specified non-zero number of
    /// milliseconds.
    pub const fn from_nonzero_millis(millis: NonZeroU64) -> NonZeroDuration {
        NonZeroDuration(Duration::from_millis(millis.get()))
    }

    /// Creates a new `NonZeroDuration` from the specified number of
    /// microseconds if that number is non-zero.
    pub const fn from_micros(micros: u64) -> Option<NonZeroDuration> {
        NonZeroDuration::new(Duration::from_micros(micros))
    }

    /// Creates a new `NonZeroDuration` from the specified non-zero number of
    /// microseconds.
    pub const fn from_nonzero_micros(micros: NonZeroU64) -> NonZeroDuration {
        NonZeroDuration(Duration::from_micros(micros.get()))
    }

    /// Creates a new `NonZeroDuration` from the specified number of nanoseconds
    /// if that number is non-zero.
    pub const fn from_nanos(nanos: u64) -> Option<NonZeroDuration> {
        NonZeroDuration::new(Duration::from_nanos(nanos))
    }

    /// Creates a new `NonZeroDuration` from the specified non-zero number of
    /// nanoseconds.
    pub const fn from_nonzero_nanos(nanos: NonZeroU64) -> NonZeroDuration {
        NonZeroDuration(Duration::from_nanos(nanos.get()))
    }

    /// Creates a non-zero if the given value is not zero.
    pub const fn new(d: Duration) -> Option<NonZeroDuration> {
        // Can't do this comparison as `d == Duration::from_secs(0)` because
        // equality checking is not const.
        if d.as_nanos() == 0 {
            return None;
        }

        Some(NonZeroDuration(d))
    }

    /// Like [`Duration::saturating_add`].
    pub fn saturating_add(self, d: Duration) -> Self {
        // NB: Duration is strictly positive so we're not violating the
        // invariant.
        Self(self.0.saturating_add(d))
    }

    /// Like [`Duration::saturating_mul`].
    pub fn saturating_mul(self, m: NonZeroU32) -> Self {
        // NB: multiplier is nonzero so we're not violating the invariant.
        Self(self.0.saturating_mul(m.into()))
    }

    /// Returns the value as a [`Duration`].
    pub const fn get(self) -> Duration {
        self.0
    }
}

impl From<NonZeroDuration> for Duration {
    fn from(NonZeroDuration(d): NonZeroDuration) -> Duration {
        d
    }
}

impl core::ops::Add<Duration> for NonZeroDuration {
    type Output = Self;

    fn add(self, rhs: Duration) -> Self::Output {
        let Self(d) = self;
        Self(d + rhs)
    }
}

impl core::ops::Add<NonZeroDuration> for NonZeroDuration {
    type Output = Self;

    fn add(self, rhs: NonZeroDuration) -> Self::Output {
        self + rhs.get()
    }
}

impl core::ops::Mul<NonZeroU32> for NonZeroDuration {
    type Output = Self;

    fn mul(self, rhs: NonZeroU32) -> Self::Output {
        let Self(d) = self;
        Self(d * rhs.get())
    }
}

/// Rounds `x` up to the next multiple of 4 unless `x` is already a multiple of
/// 4.
pub(crate) fn round_to_next_multiple_of_four(x: usize) -> usize {
    (x + 3) & !3
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn non_zero_duration() {
        // `NonZeroDuration` should not hold a zero duration.
        assert_eq!(NonZeroDuration::new(Duration::from_secs(0)), None);

        // `new_unchecked` should hold the duration as is.
        let d = Duration::from_secs(1);
        assert_eq!(unsafe { NonZeroDuration::new_unchecked(d) }, NonZeroDuration(d));

        // `NonZeroDuration` should hold a non-zero duration.
        let non_zero = NonZeroDuration::new(d);
        assert_eq!(non_zero, Some(NonZeroDuration(d)));

        let one_u64 = NonZeroU64::new(1).unwrap();
        let expect = NonZeroDuration(Duration::from_secs(1));
        assert_eq!(NonZeroDuration::from_secs(1), Some(expect));
        assert_eq!(NonZeroDuration::from_nonzero_secs(one_u64), expect);

        let expect = NonZeroDuration(Duration::new(1, 1));
        assert_eq!(NonZeroDuration::from_secs_nanos(1, 1), Some(expect));
        assert_eq!(
            NonZeroDuration::from_nonzero_secs_nanos(one_u64, NonZeroU32::new(1).unwrap()),
            expect
        );

        let expect = NonZeroDuration(Duration::from_millis(1));
        assert_eq!(NonZeroDuration::from_millis(1), Some(expect));
        assert_eq!(NonZeroDuration::from_nonzero_millis(one_u64), expect);

        let expect = NonZeroDuration(Duration::from_micros(1));
        assert_eq!(NonZeroDuration::from_micros(1), Some(expect));
        assert_eq!(NonZeroDuration::from_nonzero_micros(one_u64), expect);

        let expect = NonZeroDuration(Duration::from_nanos(1));
        assert_eq!(NonZeroDuration::from_nanos(1), Some(expect));
        assert_eq!(NonZeroDuration::from_nonzero_nanos(one_u64), expect);

        // `get` and `Into::into` should return the underlying duration.
        let non_zero = non_zero.unwrap();
        assert_eq!(d, non_zero.get());
        assert_eq!(d, non_zero.into());
    }

    #[test]
    fn test_next_multiple_of_four() {
        for x in 0usize..=(core::u16::MAX - 3) as usize {
            let y = round_to_next_multiple_of_four(x);
            assert_eq!(y % 4, 0);
            assert!(y >= x);
            if x % 4 == 0 {
                assert_eq!(x, y);
            } else {
                assert_eq!(x + (4 - x % 4), y);
            }
        }
    }

    #[test]
    fn add_duration() {
        let a = NonZeroDuration::new(Duration::from_secs(48291)).unwrap();
        let b = Duration::from_secs(195811);
        assert_eq!(Some(a + b), NonZeroDuration::new(a.get() + b));

        assert_eq!(a + Duration::from_secs(0), a);
    }

    #[test]
    fn add_nonzero_duration() {
        let a = NonZeroDuration::new(Duration::from_secs(48291)).unwrap();
        let b = NonZeroDuration::new(Duration::from_secs(195811)).unwrap();
        assert_eq!(Some(a + b), NonZeroDuration::new(a.get() + b.get()));
    }
}