pub trait Decode<'a>: Sized {
type Error: From<Incomplete> + From<TrailingBytes>;
// Required method
fn decode(buf: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error>;
// Provided method
fn decode_exact(buf: &'a [u8]) -> Result<Self, Self::Error> { ... }
}Expand description
RX-side: zero-copy decode borrowing from buf. The value is valid only as long as
buf lives.
Required Associated Types§
Sourcetype Error: From<Incomplete> + From<TrailingBytes>
type Error: From<Incomplete> + From<TrailingBytes>
Per-implementation error; constructible from Incomplete and TrailingBytes
so the fixed-width leaf read helpers (read_u8, read_u16_be,
read_array, …) and the decode_exact default lift through ?.
The variable-width helpers read_be_uint and
read_be_uint_into return
ReadUintError instead; to call them inside
decode with ?, additionally implement
From<ReadUintError> for your error (match both arms — see the
error pattern in MIGRATION.md).
Required Methods§
Provided Methods§
Sourcefn decode_exact(buf: &'a [u8]) -> Result<Self, Self::Error>
fn decode_exact(buf: &'a [u8]) -> Result<Self, Self::Error>
Decode requiring the ENTIRE buffer to be consumed. Use this at a message boundary where the length is already known.
Do NOT use it where a buffer may legally hold more than one message
(e.g. a multi-message datagram): there, trailing bytes are the next
message, not an error — use decode and thread the
remainder.
§Errors
TrailingBytes (via Self::Error) if bytes remain, plus anything
decode can return.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".