Trait WireWritePart

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

Serialization from a data type to a portion of the wire smaller than the full size of the data type.

A type that implements this trait guarantees that at least a subset of its values can be serialized into a specified number of bytes (greater than zero but less than the size of the type). The serialized bytes are written to the supplied WireCursorMut. For values that cannot be serialized into the number of bytes specified, the WireError::InvalidData error should be returned.

This trait is most useful for writing integer values to the 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 for the operation to succeed.

Types implementing this trait should also implement WireWrite for the case where the specified number of bytes is equal to the size of the type.

Required Methods§

Source

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

Serializes the data type into exactly L bytes on the 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::{WireError, WireWriter};

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 u16 were a value less than or equal to 0xFF, 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 WireWritePart for u16

Source§

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

Source§

impl WireWritePart for u32

Source§

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

Source§

impl WireWritePart for u64

Source§

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

Source§

impl WireWritePart for u128

Source§

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

Source§

impl WireWritePart for usize

Source§

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

Implementors§