bitcode/derive/
duration.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
use crate::coder::{Buffer, Decoder, Encoder, Result, View};
use crate::{Decode, Encode};
use alloc::vec::Vec;
use bytemuck::CheckedBitPattern;
use core::num::NonZeroUsize;
use core::time::Duration;

#[derive(Default)]
pub struct DurationEncoder {
    secs: <u64 as Encode>::Encoder,
    subsec_nanos: <u32 as Encode>::Encoder,
}
impl Encoder<Duration> for DurationEncoder {
    #[inline(always)]
    fn encode(&mut self, t: &Duration) {
        self.secs.encode(&t.as_secs());
        self.subsec_nanos.encode(&t.subsec_nanos());
    }
}
impl Buffer for DurationEncoder {
    fn collect_into(&mut self, out: &mut Vec<u8>) {
        self.secs.collect_into(out);
        self.subsec_nanos.collect_into(out);
    }

    fn reserve(&mut self, additional: NonZeroUsize) {
        self.secs.reserve(additional);
        self.subsec_nanos.reserve(additional);
    }
}
impl Encode for Duration {
    type Encoder = DurationEncoder;
}

/// A u32 guaranteed to be < 1 billion. Prevents Duration::new from panicking.
#[derive(Copy, Clone)]
#[repr(transparent)]
struct Nanoseconds(u32);
// Safety: u32 and Nanoseconds have the same layout since Nanoseconds is #[repr(transparent)].
unsafe impl CheckedBitPattern for Nanoseconds {
    type Bits = u32;
    #[inline(always)]
    fn is_valid_bit_pattern(bits: &Self::Bits) -> bool {
        *bits < 1_000_000_000
    }
}
impl<'a> Decode<'a> for Nanoseconds {
    type Decoder = crate::int::CheckedIntDecoder<'a, Nanoseconds, u32>;
}

#[derive(Default)]
pub struct DurationDecoder<'a> {
    secs: <u64 as Decode<'a>>::Decoder,
    subsec_nanos: <Nanoseconds as Decode<'a>>::Decoder,
}
impl<'a> View<'a> for DurationDecoder<'a> {
    fn populate(&mut self, input: &mut &'a [u8], length: usize) -> Result<()> {
        self.secs.populate(input, length)?;
        self.subsec_nanos.populate(input, length)?;
        Ok(())
    }
}
impl<'a> Decoder<'a, Duration> for DurationDecoder<'a> {
    #[inline(always)]
    fn decode(&mut self) -> Duration {
        let secs = self.secs.decode();
        let Nanoseconds(subsec_nanos) = self.subsec_nanos.decode();
        // Makes Duration::new 4x faster since it can skip checks and division.
        // Safety: impl CheckedBitPattern for Nanoseconds guarantees this.
        unsafe {
            if !Nanoseconds::is_valid_bit_pattern(&subsec_nanos) {
                core::hint::unreachable_unchecked();
            }
        }
        Duration::new(secs, subsec_nanos)
    }
}
impl<'a> Decode<'a> for Duration {
    type Decoder = DurationDecoder<'a>;
}

#[cfg(test)]
mod tests {
    #[test]
    fn test() {
        assert!(crate::decode::<Duration>(&crate::encode(&(u64::MAX, 999_999_999))).is_ok());
        assert!(crate::decode::<Duration>(&crate::encode(&(u64::MAX, 1_000_000_000))).is_err());
    }

    use alloc::vec::Vec;
    use core::time::Duration;
    fn bench_data() -> Vec<Duration> {
        crate::random_data(1000)
            .into_iter()
            .map(|(s, n): (_, u32)| Duration::new(s, n % 1_000_000_000))
            .collect()
    }
    crate::bench_encode_decode!(duration_vec: Vec<_>);
}