pub trait SerializeString {
// Required methods
fn serialize_utf8(
&self,
buffer: &mut BytesMut,
len: Option<LengthField>,
) -> Result<usize, SerializeError>;
fn serialize_utf16_be(
&self,
buffer: &mut BytesMut,
len: Option<LengthField>,
) -> Result<usize, SerializeError>;
fn serialize_utf16_le(
&self,
buffer: &mut BytesMut,
len: Option<LengthField>,
) -> Result<usize, SerializeError>;
}Expand description
Serialize strings of various encodings into a SOME/IP byte stream.
The protocol specifies three encodings (UTF-8, UTF-16 Little-Endian, and UTF-16 Big-Endian) which require adding Byte-Order-Marks and Delimiters to the start and end of the data stream, respectively.
This trait provides one method for serializing strings using each of those encodings.
Required Methods§
Sourcefn serialize_utf8(
&self,
buffer: &mut BytesMut,
len: Option<LengthField>,
) -> Result<usize, SerializeError>
fn serialize_utf8( &self, buffer: &mut BytesMut, len: Option<LengthField>, ) -> Result<usize, SerializeError>
Serializes the string into the buffer using UTF-8 encoding.
A length field can be specified to indicate 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, or the length of the serialized data exceeding the capacity of the length field.
§Examples
use rsomeip_bytes::{SerializeString, BytesMut};
let mut buffer = BytesMut::with_capacity(10);
assert_eq!("Hello!".serialize_utf8(&mut buffer, None), Ok(10));
assert_eq!(
&buffer.freeze()[..],
[
0xef_u8, 0xbb, 0xbf, // UTF-8 Byte Order Mark
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, // Hello!
0x00 // Delimiter
]
);Sourcefn serialize_utf16_be(
&self,
buffer: &mut BytesMut,
len: Option<LengthField>,
) -> Result<usize, SerializeError>
fn serialize_utf16_be( &self, buffer: &mut BytesMut, len: Option<LengthField>, ) -> Result<usize, SerializeError>
Serializes the string into the buffer using UTF-16 Big-Endian encoding.
A length field can be specified to indicate 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, or the length of the serialized data exceeding the capacity of the length field.
§Examples
use rsomeip_bytes::{SerializeString, BytesMut};
let mut buffer = BytesMut::with_capacity(12);
assert_eq!("语言处理".serialize_utf16_be(&mut buffer, None), Ok(12));
assert_eq!(
&buffer.freeze()[..],
[
0xfe_u8, 0xff, // UTF-16 Big Endian Byte Order Mark
0x8b, 0xed, 0x8a, 0x00, 0x59, 0x04, 0x74, 0x06, // 语言处理
0x00, 0x00, // Delimiter
]
);Sourcefn serialize_utf16_le(
&self,
buffer: &mut BytesMut,
len: Option<LengthField>,
) -> Result<usize, SerializeError>
fn serialize_utf16_le( &self, buffer: &mut BytesMut, len: Option<LengthField>, ) -> Result<usize, SerializeError>
Serializes the string into the buffer using UTF-16 Little-Endian encoding.
A length field can be specified to indicate 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, or the length of the serialized data exceeding the capacity of the length field.
§Examples
use rsomeip_bytes::{SerializeString, BytesMut};
let mut buffer = BytesMut::with_capacity(12);
assert_eq!("语言处理".serialize_utf16_le(&mut buffer, None), Ok(12));
assert_eq!(
&buffer.freeze()[..],
[
0xff_u8, 0xfe, // UTF-16 Little Endian Byte Order Mark
0xed, 0x8b, 0x00, 0x8a, 0x04, 0x59, 0x06, 0x74, // 语言处理
0x00, 0x00, // Delimiter
]
);