protobin/wire/
wire_i64.rs

1/// Protobuf I64 wire type (fixed 64 bit values) used to
2/// encode the Protobuf types `fixed64`, `sfixed64` and `double`.
3#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
4pub struct WireI64(pub u64);
5
6impl WireI64 {
7    /// Interpret the i64 as a Protobuf `fixed64`.
8    #[inline]
9    pub fn as_fixed64(&self) -> u64 {
10        self.0
11    }
12
13    /// Interpret the i64 as a Protobuf `sfixed64`.
14    #[inline]
15    pub fn as_sfixed64(&self) -> i64 {
16        i64::from_ne_bytes(self.0.to_ne_bytes())
17    }
18
19    /// Interpret the i64 as a Protobuf `double`.
20    #[inline]
21    pub fn as_double(&self) -> f64 {
22        f64::from_ne_bytes(self.0.to_ne_bytes())
23    }
24}
25
26#[cfg(test)]
27mod test {
28    use crate::wire::*;
29    use proptest::prelude::*;
30
31    proptest! {
32        #[test]
33        fn as_fixed64(
34            value in any::<u64>()
35        ) {
36            prop_assert_eq!(WireI64(value).as_fixed64(), value);
37        }
38    }
39
40    proptest! {
41        #[test]
42        fn as_sfixed64(
43            value in any::<u64>()
44        ) {
45            prop_assert_eq!(
46                WireI64(value).as_sfixed64(),
47                i64::from_ne_bytes(value.to_ne_bytes())
48            );
49        }
50    }
51
52    proptest! {
53        #[test]
54        fn as_double(
55            value in any::<u64>()
56        ) {
57            prop_assert_eq!(
58                WireI64(value).as_double().to_ne_bytes(),
59                f64::from_ne_bytes(value.to_ne_bytes()).to_ne_bytes()
60            );
61        }
62    }
63}