netlink_packet_utils/traits.rs
1// SPDX-License-Identifier: MIT
2
3/// A type that implements `Emitable` can be serialized.
4pub trait Emitable {
5 /// Return the length of the serialized data.
6 fn buffer_len(&self) -> usize;
7
8 /// Serialize this types and write the serialized data into the given
9 /// buffer.
10 ///
11 /// # Panic
12 ///
13 /// This method panic if the buffer is not big enough. You **must** make
14 /// sure the buffer is big enough before calling this method. You can
15 /// use [`buffer_len()`](trait.Emitable.html#method.buffer_len) to check
16 /// how big the storage needs to be.
17 fn emit(&self, buffer: &mut [u8]);
18}
19
20/// A `Parseable` type can be used to deserialize data from the type `T` for
21/// which it is implemented.
22pub trait Parseable<T>
23where
24 Self: Sized,
25 T: ?Sized,
26{
27 type Error;
28
29 /// Deserialize the current type.
30 fn parse(buf: &T) -> Result<Self, Self::Error>;
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 type Error;
41
42 /// Deserialize the current type.
43 fn parse_with_param(buf: &T, params: P) -> Result<Self, Self::Error>;
44}