Skip to main content

StaticString

Struct StaticString 

Source
pub struct StaticString<const N: usize, Encoding, Value> { /* private fields */ }
Expand description

Statically sizes string.

Serialized static strings always have the same length regardless of the size of the actual string.

Extra space is padded with delimiters.

§Examples

use rsomeip_bytes::{StaticString, Utf8, Serialize as _, Deserialize as _};

// A `StaticString` can be used to always write the same amount of data regardless
// of the size of the string.
let value = String::from("rsomeip");
let string = StaticString::<15, Utf8, _>::from(&value);

// Size is the capacity of the underlying array.
assert_eq!(Some(15), string.size());
assert_eq!(Some(15), StaticString::<15, Utf8, String>::size_hint());

// Buffer can be any type that implements `BufMut`.
let mut buffer = [0_u8; 15];

// Serialized data includes Byte Order Mark, delimiter, and padding.
assert_eq!(Ok(15), string.serialize(&mut buffer.as_mut_slice()));
assert_eq!(buffer, [
        0xef_u8, 0xbb, 0xbf, // UTF-8 BOM
        0x72, 0x73, 0x6f, 0x6d, 0x65, 0x69, 0x70, // "rsomeip"
        0x00, // Delimiter
        0x00, 0x00, 0x00, 0x00, // Padding
    ]);

// Deserialization automatically strips the BOM, delimiter, and padding.
let output = StaticString::<15, Utf8, String>::deserialize(&mut buffer.as_slice())?;
assert_eq!(output, value);

Trait Implementations§

Source§

impl<Encoding, Value, const N: usize> Deserialize for StaticString<N, Encoding, Value>
where Encoding: Encoding, Value: From<String>,

Source§

type Output = Value

Type of the deserialized data.
Source§

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

Deserializes an instance of Deserialize::Output from the buffer. Read more
Source§

fn size_hint() -> Option<usize>

Returns the minimum size of a correctly serialized Self::Output. Read more
Source§

impl<Encoding, Value, const N: usize> From<Value> for StaticString<N, Encoding, Value>
where Encoding: Encoding, Value: AsRef<str>,

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl<Encoding, Value, const N: usize> Serialize for StaticString<N, Encoding, Value>
where Encoding: Encoding, Value: AsRef<str>,

Source§

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

Serializes self into the given buffer. Read more
Source§

fn size(&self) -> Option<usize>

Returns the size of self when serialized. Read more
Source§

fn to_bytes(&self) -> Result<Bytes, SerializeError>

Returns self serialized into Bytes. Read more

Auto Trait Implementations§

§

impl<const N: usize, Encoding, Value> Freeze for StaticString<N, Encoding, Value>
where Value: Freeze,

§

impl<const N: usize, Encoding, Value> RefUnwindSafe for StaticString<N, Encoding, Value>
where Value: RefUnwindSafe, Encoding: RefUnwindSafe,

§

impl<const N: usize, Encoding, Value> Send for StaticString<N, Encoding, Value>
where Value: Send, Encoding: Send,

§

impl<const N: usize, Encoding, Value> Sync for StaticString<N, Encoding, Value>
where Value: Sync, Encoding: Sync,

§

impl<const N: usize, Encoding, Value> Unpin for StaticString<N, Encoding, Value>
where Value: Unpin, Encoding: Unpin,

§

impl<const N: usize, Encoding, Value> UnsafeUnpin for StaticString<N, Encoding, Value>
where Value: UnsafeUnpin,

§

impl<const N: usize, Encoding, Value> UnwindSafe for StaticString<N, Encoding, Value>
where Value: UnwindSafe, Encoding: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.