pub trait Decode {
// Required methods
fn read_byte(&mut self) -> Result<u8>;
fn read_into(&mut self, out: &mut [u8]) -> Result<()>;
fn max_alloc(&self) -> usize;
// Provided methods
fn read_varint_u64(&mut self) -> Result<u64> { ... }
fn read_varint_u128(&mut self) -> Result<u128> { ... }
fn read_length_prefixed(&mut self) -> Result<Vec<u8>> { ... }
}Expand description
Source that a Deserialize implementation reads its wire-format bytes
from.
Implemented by every concrete decoder in the crate (Decoder for the
in-memory case, crate::IoDecoder for std::io::Read streams). User
code rarely implements Decode directly — Deserialize impls are
written generically over D: Decode.
All methods are total: on any byte sequence they either succeed
(advancing the cursor) or return a SerialError. They never panic,
never read past the input, and never allocate more memory than
Decode::max_alloc permits.
Required Methods§
Sourcefn read_byte(&mut self) -> Result<u8>
fn read_byte(&mut self) -> Result<u8>
Read the next byte, advancing the cursor.
§Errors
Returns SerialError::UnexpectedEof if the input is exhausted.
Streaming decoders MAY return an I/O-flavoured error variant.
Sourcefn read_into(&mut self, out: &mut [u8]) -> Result<()>
fn read_into(&mut self, out: &mut [u8]) -> Result<()>
Fill out with exactly out.len() bytes, advancing the cursor.
§Errors
Returns SerialError::UnexpectedEof on short read.
Sourcefn max_alloc(&self) -> usize
fn max_alloc(&self) -> usize
Maximum number of bytes the decoder will allocate for a single
length-prefixed value. Mirrors Config::max_alloc.
Provided Methods§
Sourcefn read_varint_u64(&mut self) -> Result<u64>
fn read_varint_u64(&mut self) -> Result<u64>
Read a LEB128 varint as a u64.
§Errors
Returns SerialError::VarintOverflow for an overlong encoding,
or SerialError::UnexpectedEof for a truncated one.
Sourcefn read_varint_u128(&mut self) -> Result<u128>
fn read_varint_u128(&mut self) -> Result<u128>
Sourcefn read_length_prefixed(&mut self) -> Result<Vec<u8>>
fn read_length_prefixed(&mut self) -> Result<Vec<u8>>
Read a length-prefixed byte run, allocating a fresh Vec<u8>.
The length is read as a varint, validated against
Decode::max_alloc, then the corresponding number of bytes is
read from the underlying source.
§Errors
SerialError::InvalidLengthif the prefix exceedsmax_alloc.SerialError::UnexpectedEofif the source runs out before the declared length is satisfied.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".