netlink_packet_utils/traits.rs
1// SPDX-License-Identifier: MIT
2
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
11 /// buffer.
12 ///
13 /// # Panic
14 ///
15 /// This method panic if the buffer is not big enough. You **must** make
16 /// sure the buffer is big enough before calling this method. You can
17 /// use [`buffer_len()`](trait.Emitable.html#method.buffer_len) to check
18 /// how big the storage needs to be.
19 fn emit(&self, buffer: &mut [u8]);
20}
21
22/// A `Parseable` type can be used to deserialize data from the type `T` for
23/// which it is implemented.
24pub trait Parseable<T>
25where
26 Self: Sized,
27 T: ?Sized,
28{
29 /// Deserialize the current type.
30 fn parse(buf: &T) -> Result<Self, DecodeError>;
31}
32
33/// A `Parseable` type can be used to deserialize data from the type `T` for
34/// which it is implemented.
35pub trait ParseableParametrized<T, P>
36where
37 Self: Sized,
38 T: ?Sized,
39{
40 /// Deserialize the current type.
41 fn parse_with_param(buf: &T, params: P) -> Result<Self, DecodeError>;
42}