Skip to main content

Encoding

Trait Encoding 

Source
pub trait Encoding: Sealed {
    // Required methods
    fn serialize<Value, Buffer>(
        value: Value,
        buffer: &mut Buffer,
    ) -> Result<usize, SerializeError>
       where Value: AsRef<str>,
             Buffer: BufMut + ?Sized;
    fn size<Value>(value: &Value) -> Option<usize>
       where Value: AsRef<str> + ?Sized;
    fn deserialize<Buffer>(
        buffer: &mut Buffer,
    ) -> Result<String, DeserializeError>
       where Buffer: Buf + ?Sized;
}
Expand description

String encoding according to the SOME/IP on-wire format.

Required Methods§

Source

fn serialize<Value, Buffer>( value: Value, buffer: &mut Buffer, ) -> Result<usize, SerializeError>
where Value: AsRef<str>, Buffer: BufMut + ?Sized,

Serializes the value into the given buffer.

Includes a Byte Order Mark and a Delimiter before and after the actual string, respectively.

The string must not contain any null characters.

Returns the length of the serialized data.

§Errors

Returns a SerializeError if the serialization fails. Some data may still be written to the buffer if an error occurs.

§Examples
use rsomeip_bytes::{Encoding as _, Utf8};

// The buffer can be any type that implements `BufMut`.
let mut buffer = [0_u8; 11];
let size = Utf8::serialize("rsomeip", &mut buffer.as_mut_slice())?;

// Size includes the size of the Byte Order Mark, string and delimiter.
assert_eq!(size, 11);
assert_eq!(buffer.as_slice(), [
        0xef_u8, 0xbb, 0xbf, // UTF-8 BOM
        0x72, 0x73, 0x6f, 0x6d, 0x65, 0x69, 0x70, // "rsomeip"
        0x00, // Delimiter
    ].as_slice());
Source

fn size<Value>(value: &Value) -> Option<usize>
where Value: AsRef<str> + ?Sized,

Returns the size of the value when serialized.

Includes the size of the Byte Order Mark and the Delimiter.

Returns None if the size is out of bounds.

§Examples
use rsomeip_bytes::{Encoding as _, Utf8, Utf16BE, Utf16LE};

// Size includes the size of the Byte Order Mark, string and delimiter.
assert_eq!(Utf8::size("rsomeip"), Some(11));
assert_eq!(Utf16BE::size("rsomeip"), Some(18));
assert_eq!(Utf16LE::size("rsomeip"), Some(18));
Source

fn deserialize<Buffer>(buffer: &mut Buffer) -> Result<String, DeserializeError>
where Buffer: Buf + ?Sized,

Deserializes a null-terminated String from the given buffer.

Expects a Byte Order Mark and a Delimiter at the start and end of the string, respectively.

§Errors

Returns a DeserializeError if the deserialization fails.

Specifically, deserialization fails if the string is not null-terminated or if there is a null character before the end of the string.

§Examples
use rsomeip_bytes::{Encoding as _, Utf8};

// The buffer can be any type that implements `Buf`.
let buffer = [
    0xef_u8, 0xbb, 0xbf, // UTF-8 BOM
    0x72, 0x73, 0x6f, 0x6d, 0x65, 0x69, 0x70, // "rsomeip"
    0x00, // Delimiter
];

let string = Utf8::deserialize(&mut buffer.as_slice())?;
assert_eq!(&string, "rsomeip");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§