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:
- Serialized into raw bytes (
Marshaler::marshal) - Deserialized from raw bytes (
Marshaler::unmarshal)
Each payload type corresponds to exactly one command ID.
Required Associated Constants§
Sourceconst PAYLOAD_SIZE: u16
const PAYLOAD_SIZE: u16
Expected size of the payload in bytes.
Required Methods§
Sourcefn marshal(&self, dst: &mut [u8]) -> Result<usize, MarshalerError>
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.
Sourcefn unmarshal(raw: &[u8]) -> Result<Self, MarshalerError>
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.