falco_event/events/to_bytes.rs
1use std::io::Write;
2
3/// Trait for converting events to a byte representation.
4///
5/// This trait is implemented for types that can be serialized into a byte array representing
6/// an event. It has the same methods as [`crate::events::payload::PayloadToBytes`], but is a separate
7/// trait to disallow serializing raw payloads that are not events by mistake.
8pub trait EventToBytes {
9 /// Get the binary size of the event.
10 fn binary_size(&self) -> usize;
11
12 /// Write the event to a writer implementing `[std::io::Write]`.
13 fn write<W: Write>(&self, writer: W) -> std::io::Result<()>;
14}
15
16impl EventToBytes for &[u8] {
17 #[inline]
18 fn binary_size(&self) -> usize {
19 self.len()
20 }
21
22 #[inline]
23 fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
24 writer.write_all(self)
25 }
26}