falco_event/types/primitive/
integers.rs1use crate::fields::{FromBytes, FromBytesError, ToBytes};
2
3macro_rules! impl_int_type {
4 ($ty:ty) => {
5 impl FromBytes<'_> for $ty {
6 #[inline]
7 fn from_bytes(buf: &mut &[u8]) -> Result<Self, FromBytesError>
8 where
9 Self: Sized,
10 {
11 let value_buf = buf.split_off(..std::mem::size_of::<$ty>()).ok_or_else(|| {
12 FromBytesError::TruncatedField {
13 wanted: std::mem::size_of::<$ty>(),
14 got: buf.len(),
15 }
16 })?;
17 Ok(<$ty>::from_ne_bytes(value_buf.try_into().unwrap()))
18 }
19 }
20
21 impl ToBytes for $ty {
22 #[inline]
23 fn binary_size(&self) -> usize {
24 std::mem::size_of::<$ty>()
25 }
26
27 #[inline]
28 fn write<W: std::io::Write>(&self, mut writer: W) -> std::io::Result<()> {
29 writer.write_all(self.to_ne_bytes().as_slice())
30 }
31
32 #[inline]
33 fn default_repr() -> impl ToBytes {
34 0 as $ty
35 }
36 }
37 };
38}
39
40impl_int_type!(u8);
41impl_int_type!(i8);
42impl_int_type!(u16);
43impl_int_type!(i16);
44impl_int_type!(u32);
45impl_int_type!(i32);
46impl_int_type!(u64);
47impl_int_type!(i64);