|
| 1 | +use bincode::{Decode, Encode}; |
| 2 | + |
| 3 | +use crate::{Monotonic, Timestamp}; |
| 4 | + |
| 5 | +// |
| 6 | +// Timestamp |
| 7 | +// |
| 8 | +#[cfg(not(feature = "as-float-secs"))] |
| 9 | +impl Encode for Timestamp { |
| 10 | + fn encode<E: bincode::enc::Encoder>( |
| 11 | + &self, |
| 12 | + encoder: &mut E, |
| 13 | + ) -> Result<(), bincode::error::EncodeError> { |
| 14 | + let nanos: u64 = self.as_nanos().try_into().unwrap(); |
| 15 | + nanos.encode(encoder) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[cfg(feature = "as-float-secs")] |
| 20 | +impl Encode for Timestamp { |
| 21 | + fn encode<E: bincode::enc::Encoder>( |
| 22 | + &self, |
| 23 | + encoder: &mut E, |
| 24 | + ) -> Result<(), bincode::error::EncodeError> { |
| 25 | + let secs = self.as_secs_f64(); |
| 26 | + secs.encode(encoder) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[cfg(not(feature = "as-float-secs"))] |
| 31 | +impl<C> Decode<C> for Timestamp { |
| 32 | + fn decode<D: bincode::de::Decoder<Context = C>>( |
| 33 | + decoder: &mut D, |
| 34 | + ) -> Result<Self, bincode::error::DecodeError> { |
| 35 | + let nanos = u64::decode(decoder)?; |
| 36 | + Ok(nanos.into()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(feature = "as-float-secs")] |
| 41 | +impl<C> Decode<C> for Timestamp { |
| 42 | + fn decode<D: bincode::de::Decoder<Context = C>>( |
| 43 | + decoder: &mut D, |
| 44 | + ) -> Result<Self, bincode::error::DecodeError> { |
| 45 | + let secs = f64::decode(decoder)?; |
| 46 | + Ok(Timestamp::from_secs_f64(secs)) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// |
| 51 | +// Monotonic |
| 52 | +// |
| 53 | +impl Encode for Monotonic { |
| 54 | + fn encode<E: bincode::enc::Encoder>( |
| 55 | + &self, |
| 56 | + encoder: &mut E, |
| 57 | + ) -> Result<(), bincode::error::EncodeError> { |
| 58 | + let nanos: u64 = self.as_nanos().try_into().unwrap(); |
| 59 | + nanos.encode(encoder) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<C> Decode<C> for Monotonic { |
| 64 | + fn decode<D: bincode::de::Decoder<Context = C>>( |
| 65 | + decoder: &mut D, |
| 66 | + ) -> Result<Self, bincode::error::DecodeError> { |
| 67 | + let nanos = u64::decode(decoder)?; |
| 68 | + Ok(nanos.into()) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::{Monotonic, Timestamp}; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_timestamp_bincode() { |
| 78 | + let ts = Timestamp::from_nanos(1_500_000_000); |
| 79 | + let encoded = bincode::encode_to_vec(ts, bincode::config::standard()).unwrap(); |
| 80 | + let (decoded, _) = |
| 81 | + bincode::decode_from_slice(&encoded, bincode::config::standard()).unwrap(); |
| 82 | + assert_eq!(ts, decoded); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_monotonic_bincode() { |
| 87 | + let mono = Monotonic::from_nanos(2_500_000_000); |
| 88 | + let encoded = bincode::encode_to_vec(mono, bincode::config::standard()).unwrap(); |
| 89 | + let (decoded, _) = |
| 90 | + bincode::decode_from_slice(&encoded, bincode::config::standard()).unwrap(); |
| 91 | + assert_eq!(mono, decoded); |
| 92 | + } |
| 93 | +} |
0 commit comments