pub trait Length: Sealed {
// Required methods
fn serialize<Buffer, Value>(
value: &Value,
buffer: &mut Buffer,
) -> Result<usize, SerializeError>
where Buffer: BufMut + ?Sized,
Value: Serialize;
fn serialize_with<Buffer, Value, SerializeFn, SizeFn>(
serialize: SerializeFn,
size: SizeFn,
value: Value,
buffer: &mut Buffer,
) -> Result<usize, SerializeError>
where Buffer: BufMut + ?Sized,
for<'any> SerializeFn: Fn(&Value, &mut dyn BufMut) -> Result<usize, SerializeError>,
for<'any> SizeFn: Fn(&Value) -> Option<usize>;
fn deserialize<Buffer, Value>(
buffer: &mut Buffer,
) -> Result<Value::Output, DeserializeError>
where Buffer: Buf + ?Sized,
Value: Deserialize;
fn deserialize_with<Buffer, Value, Function>(
function: Function,
buffer: &mut Buffer,
) -> Result<Value, DeserializeError>
where Buffer: Buf + ?Sized,
for<'any> Function: Fn(&mut dyn Buf) -> Result<Value, DeserializeError>;
fn size() -> usize;
fn capacity() -> Option<usize>;
}Expand description
Length of a value.
Encodes the length of a dynamically sized type in the SOME/IP on-wire format.
Required Methods§
Sourcefn serialize<Buffer, Value>(
value: &Value,
buffer: &mut Buffer,
) -> Result<usize, SerializeError>
fn serialize<Buffer, Value>( value: &Value, buffer: &mut Buffer, ) -> Result<usize, SerializeError>
Serializes the value into the given buffer with a length field.
Returns the size of the serialized data in bytes, including the length field itself.
§Errors
Returns a SerializeError if the serialization fails. Some data may still be written to
the buffer if an error occurs.
§Panics
Panics if the buffer doesn’t have enough capacity for the serialized type. It’s advised to
ensure that the buffer has at least Serialize::size capacity.
§Examples
use rsomeip_bytes::{LengthU32, Length as _, BytesMut};
let mut buffer = BytesMut::with_capacity(6);
// Serialize a value with a 32-bit length field into the buffer.
let size = LengthU32::serialize(&0x0102_u16, &mut buffer)?;
// Size includes length and value.
assert_eq!(size, 6);
// Length field comes before the value.
assert_eq!(&buffer.freeze(), [0_u8, 0, 0, 2, 1, 2].as_slice());Sourcefn serialize_with<Buffer, Value, SerializeFn, SizeFn>(
serialize: SerializeFn,
size: SizeFn,
value: Value,
buffer: &mut Buffer,
) -> Result<usize, SerializeError>
fn serialize_with<Buffer, Value, SerializeFn, SizeFn>( serialize: SerializeFn, size: SizeFn, value: Value, buffer: &mut Buffer, ) -> Result<usize, SerializeError>
Serializes the value into the buffer with a length field using the given functions.
Returns the size of the serialized data in bytes, including the length field itself.
§Errors
Returns a SerializeError if the serialization fails. Some data may still be written to
the buffer if an error occurs.
§Panics
Panics if the buffer doesn’t have enough capacity for the serialized type. It’s advised to
ensure that the buffer has at least Serialize::size capacity.
§Examples
use rsomeip_bytes::{LengthU32, Length as _, BytesMut, Serialize as _};
// Type requiring custom serialization.
struct Foo {
bar: u8,
baz: u16,
}
let mut buffer = BytesMut::with_capacity(7);
// Serialize a value with a 32-bit length field into the buffer.
let size = LengthU32::serialize_with(
|value, mut buffer| Ok(value.bar.serialize(buffer)? + value.baz.serialize(buffer)?),
|value| Some(value.bar.size()? + value.baz.size()?),
Foo { bar: 1_u8, baz: 0x0203_u16 },
&mut buffer)?;
// Size includes length and value.
assert_eq!(size, 7);
// Length field comes before the value.
assert_eq!(&buffer.freeze(), [0_u8, 0, 0, 3, 1, 2, 3].as_slice());Sourcefn deserialize<Buffer, Value>(
buffer: &mut Buffer,
) -> Result<Value::Output, DeserializeError>
fn deserialize<Buffer, Value>( buffer: &mut Buffer, ) -> Result<Value::Output, DeserializeError>
Deserializes a value from the given buffer with a preceding length field.
The value in the length field is used to limit the deserialization of the value from the buffer.
§Errors
Returns a DeserializeError if the deserialization fails.
§Examples
use rsomeip_bytes::{Deserialize as _, LengthU32, Length as _};
// The buffer can be any type that implements `Buf`.
let buffer = [0_u8, 0, 0, 2, 1, 2];
// Deserialize a value with a 32-bit length field from the buffer.
let value = LengthU32::deserialize::<_, u16>(&mut buffer.as_slice())?;
assert_eq!(value, 0x0102_u16);Sourcefn deserialize_with<Buffer, Value, Function>(
function: Function,
buffer: &mut Buffer,
) -> Result<Value, DeserializeError>
fn deserialize_with<Buffer, Value, Function>( function: Function, buffer: &mut Buffer, ) -> Result<Value, DeserializeError>
Deserializes a value from the buffer with a preceding length field using the given
function.
The value in the length field is used to limit the deserialization of the value from the buffer.
§Errors
Returns a DeserializeError if the deserialization fails.
§Examples
use rsomeip_bytes::{Deserialize as _, LengthU32, Length as _};
// Type requiring custom deerialization.
#[derive(Debug, PartialEq, Eq)]
struct Foo {
bar: u8,
baz: u16,
}
// The buffer can be any type that implements `Buf`.
let buffer = [0_u8, 0, 0, 3, 1, 2, 3];
// Deserialize a value with a 32-bit length field from the buffer.
let value = LengthU32::deserialize_with::<_, Foo, _>(
|mut buffer| {
Ok(Foo {
bar: u8::deserialize(buffer)?,
baz: u16::deserialize(buffer)?,
})
},
&mut buffer.as_slice())?;
assert_eq!(value, Foo { bar: 1_u8, baz: 0x0203_u16 });Sourcefn size() -> usize
fn size() -> usize
Returns the size of the length field in bytes.
§Examples
use rsomeip_bytes::{Length as _, LengthZero, LengthU8, LengthU16, LengthU32};
assert_eq!(LengthZero::size(), 0);
assert_eq!(LengthU8::size(), 1);
assert_eq!(LengthU16::size(), 2);
assert_eq!(LengthU32::size(), 4);Sourcefn capacity() -> Option<usize>
fn capacity() -> Option<usize>
Returns the capacity of the length field.
This is the maximum value that the field can represent.
Returns None is the capacity is greater than usize::MAX.
§Examples
use rsomeip_bytes::{Length as _, LengthZero, LengthU8, LengthU16, LengthU32};
assert_eq!(LengthZero::capacity(), Some(usize::MAX));
assert_eq!(LengthU8::capacity(), Some(0xff));
assert_eq!(LengthU16::capacity(), Some(0xffff));
assert_eq!(LengthU32::capacity(), Some(0xffff_ffff));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".