falco_event/types/time/
duration.rs

1use crate::fields::{FromBytes, FromBytesError, ToBytes};
2use std::io::Write;
3use std::time::Duration;
4
5impl FromBytes<'_> for Duration {
6    #[inline]
7    fn from_bytes(buf: &mut &[u8]) -> Result<Self, FromBytesError>
8    where
9        Self: Sized,
10    {
11        let nanos = u64::from_bytes(buf)?;
12        Ok(Self::from_nanos(nanos))
13    }
14}
15
16impl ToBytes for Duration {
17    #[inline]
18    fn binary_size(&self) -> usize {
19        std::mem::size_of::<u64>()
20    }
21
22    #[inline]
23    fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
24        (self.as_nanos() as u64).write(writer)
25    }
26
27    #[inline]
28    fn default_repr() -> impl ToBytes {
29        0u64
30    }
31}