Skip to main content

Marshaler

Trait Marshaler 

Source
pub trait Marshaler: Sized {
    const CMD_ID: u16;
    const PAYLOAD_SIZE: u16;

    // Required methods
    fn marshal(&self, dst: &mut [u8]) -> Result<usize, MarshalerError>;
    fn unmarshal(raw: &[u8]) -> Result<Self, MarshalerError>;
}
Expand description

Payload marshaling interface.

Marshaler defines how a message payload is:

Each payload type corresponds to exactly one command ID.

Required Associated Constants§

Source

const CMD_ID: u16

Command ID associated with this payload type.

Source

const PAYLOAD_SIZE: u16

Expected size of the payload in bytes.

Required Methods§

Source

fn marshal(&self, dst: &mut [u8]) -> Result<usize, MarshalerError>

Serialize the payload into the destination buffer.

Returns the number of bytes written on success.

§Notes

If called manually, the caller must ensure that the destination buffer is large enough to hold the serialized payload.

§Errors

Returns an error if the destination buffer is too small or the payload cannot be encoded.

Source

fn unmarshal(raw: &[u8]) -> Result<Self, MarshalerError>

Deserialize a payload from raw bytes.

The input slice contains only the payload portion (no header, command ID, or CRC).

Implementations must not depend on framing details.

§Errors

Returns an error if the data is invalid or does not match the expected payload format.

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§