PackedStruct

Trait PackedStruct 

Source
pub trait PackedStruct: Sized {
    const PACKED_SIZE: usize;

    // Required methods
    fn from_packed_bytes(bytes: &[u8]) -> Option<Self>;
    fn to_packed_bytes(&self) -> Vec<u8> ;

    // Provided methods
    fn from_packed_slice(bytes: &[u8]) -> Option<Vec<Self>> { ... }
    fn to_packed_slice(structs: &[Self]) -> Vec<u8>  { ... }
}
Expand description

A trait for types that can be read from and written to packed byte representations.

This trait provides methods for handling structures that contain I24 values mixed with native types, working around the nested packing limitation.

Required Associated Constants§

Source

const PACKED_SIZE: usize

The size in bytes of the packed representation.

Required Methods§

Source

fn from_packed_bytes(bytes: &[u8]) -> Option<Self>

Reads a single instance from packed bytes.

§Arguments
  • bytes - A byte slice containing the packed representation. Must be at least PACKED_SIZE bytes long.
§Returns

The deserialized structure, or None if the input is too short.

Source

fn to_packed_bytes(&self) -> Vec<u8>

Writes the structure to a packed byte representation.

§Returns

A vector containing the packed bytes.

Provided Methods§

Source

fn from_packed_slice(bytes: &[u8]) -> Option<Vec<Self>>

Reads multiple instances from a packed byte slice.

§Arguments
  • bytes - A byte slice containing multiple packed structures. Length must be a multiple of PACKED_SIZE.
§Returns

A vector of deserialized structures, or None if the input length is not a multiple of PACKED_SIZE.

Source

fn to_packed_slice(structs: &[Self]) -> Vec<u8>

Writes multiple structures to a packed byte representation.

§Arguments
  • structs - A slice of structures to serialize.
§Returns

A vector containing all packed bytes concatenated.

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.

Implementors§