Trait VectoredWritePart

Source
pub trait VectoredWritePart: Sized {
    // Required method
    fn write_vectored_part<const L: usize, const E: bool>(
        &self,
        curs: &mut VectoredCursorMut<'_>,
    ) -> Result<(), WireError>;
}
Expand description

Serialization from an owned data type to a portion of the vectored wire smaller than what the data type would normally produce.

This trait is most useful for writing integer values to the vectored wire that can be represented in fewer bytes than what the data type is capable of storing, such as writing out a u32 to 3 bytes. The caller must ensure that the value stored in the data type can fit in the number of bytes specified.

A type implementing this trait must guarantee that any length L passed in that is greater than 0 and smaller than the size of the type can be converted into the type. Types implementing this trait should also implement WireWrite for the case where L is equal to the size of the type.

Required Methods§

Source

fn write_vectored_part<const L: usize, const E: bool>( &self, curs: &mut VectoredCursorMut<'_>, ) -> Result<(), WireError>

Serializes the data type into L bytes on the vectored wire. If the data’s value exceeds the bounds of what can be stored in L bytes, this function will return a WireError::InvalidData error.

As an example, the following function would return an error because the value contained in the u16 can’t be represented by a single byte:

use wire_rs::{WireWriter, WireError};

fn decode_partial_out_of_range() -> Result<(), WireError> {
    let mut buf = [0u8; 4];
    let out_of_range = 0x0100u16;
    let mut writer: WireWriter = WireWriter::new(buf.as_mut_slice());
    writer.write_part::<u16, 1>(&out_of_range) // Returns Err(WireError::InvalidData)
}

If the value were <= 0xFF instead, the above function would return an Ok() result.

The generic boolean E designates the intended endianness of the data being written. If E is set to true, then the data will be serialized in big endian format; if false, it will be serialized in little endian.

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.

Implementations on Foreign Types§

Source§

impl VectoredWritePart for u16

Source§

fn write_vectored_part<const L: usize, const E: bool>( &self, curs: &mut VectoredCursorMut<'_>, ) -> Result<(), WireError>

Source§

impl VectoredWritePart for u32

Source§

fn write_vectored_part<const L: usize, const E: bool>( &self, curs: &mut VectoredCursorMut<'_>, ) -> Result<(), WireError>

Source§

impl VectoredWritePart for u64

Source§

fn write_vectored_part<const L: usize, const E: bool>( &self, curs: &mut VectoredCursorMut<'_>, ) -> Result<(), WireError>

Source§

impl VectoredWritePart for u128

Source§

fn write_vectored_part<const L: usize, const E: bool>( &self, curs: &mut VectoredCursorMut<'_>, ) -> Result<(), WireError>

Source§

impl VectoredWritePart for usize

Source§

fn write_vectored_part<const L: usize, const E: bool>( &self, curs: &mut VectoredCursorMut<'_>, ) -> Result<(), WireError>

Implementors§