netlink_packet/
traits.rs

1// FIXME: should we expose these traits directly? They may be useful, but the `Emitable` trait is
2// a bit tricky since `emit()` can panic. Exposing only `Parseable` seems clumsy.
3use crate::DecodeError;
4
5/// A type that implements `Emitable` can be serialized.
6pub trait Emitable {
7    /// Return the length of the serialized data.
8    fn buffer_len(&self) -> usize;
9
10    /// Serialize this types and write the serialized data into the given buffer.
11    ///
12    /// # Panic
13    ///
14    /// This method panic if the buffer is not big enough. You **must** make sure the buffer is big
15    /// enough before calling this method. You can use
16    /// [`buffer_len()`](trait.Emitable.html#method.buffer_len) to check how big the storage needs
17    /// to be.
18    fn emit(&self, buffer: &mut [u8]);
19}
20
21/// A `Parseable` type can be used to deserialize data into the target type `T` for which it is
22/// implemented.
23pub trait Parseable<T> {
24    /// Deserialize the current type.
25    fn parse(&self) -> Result<T, DecodeError>;
26}