pub trait Serialize {
// Required methods
fn serialize(
&self,
buffer: &mut impl BufMut,
) -> Result<usize, SerializeError>;
fn size_hint(&self) -> usize;
// Provided method
fn serialize_len(
&self,
length: LengthField,
buffer: &mut impl BufMut,
) -> Result<usize, SerializeError> { ... }
}Expand description
Serialize data into a SOME/IP byte stream.
This trait provides methods for serializing data structures into a byte stream (BufMut)
encoded in the SOME/IP on-wire format.
serialize is used to serialize statically sized types, while serialize_len is used to
serialize dynamically sized types into the stream.
Required Methods§
Sourcefn serialize(&self, buffer: &mut impl BufMut) -> Result<usize, SerializeError>
fn serialize(&self, buffer: &mut impl BufMut) -> Result<usize, SerializeError>
Serializes the implementing type into the buffer.
Returns the size of the serialized data.
§Errors
This function will return an error if serialization fails for any reason, such as the buffer not having enough space.
§Examples
use rsomeip_bytes::{BytesMut, Serialize};
let mut buffer = BytesMut::with_capacity(3);
assert_eq!(1u8.serialize(&mut buffer), Ok(1));
assert_eq!(2u16.serialize(&mut buffer), Ok(2));
assert_eq!(&buffer.freeze()[..], &[1u8, 0u8, 2u8][..]);Sourcefn size_hint(&self) -> usize
fn size_hint(&self) -> usize
Returns the expected length of the serialized data.
When using serialize_len, the length field is serialized before the type itself.
This method serves as way to calculate the length of the serialized type before serializing it.
§Examples
use rsomeip_bytes::Serialize;
assert_eq!(0u8.size_hint(), 1);
assert_eq!(0u32.size_hint(), 4);
assert_eq!(vec![1u8, 2, 3].size_hint(), 3);Provided Methods§
Sourcefn serialize_len(
&self,
length: LengthField,
buffer: &mut impl BufMut,
) -> Result<usize, SerializeError>
fn serialize_len( &self, length: LengthField, buffer: &mut impl BufMut, ) -> Result<usize, SerializeError>
Serializes the implementing type into the buffer.
This method specifies a length field which is used to indicate the size of the data to be
serialized. This is necessary in case of dynamically sized data structures, like Vec.
Returns the size of the serialized data including the length field.
§Errors
This function will return an error if serialization fails for any reason, such as the buffer not having enough space, or the length of the serialized data exceeding the capacity of the length field.
§Examples
use rsomeip_bytes::{BytesMut, Serialize, LengthField};
let mut buffer = BytesMut::with_capacity(3);
let vec = vec![0u8, 1u8];
assert_eq!(vec.serialize_len(LengthField::U8, &mut buffer), Ok(3));
assert_eq!(&buffer.freeze()[..], &[2u8, 0u8, 1u8][..]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.