Skip to main content

Decode

Trait Decode 

Source
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§

Source

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.

Source

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.

Source

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§

Source

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.

Source

fn read_varint_u128(&mut self) -> Result<u128>

Read a LEB128 varint as a u128.

§Errors

See Decode::read_varint_u64.

Source

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

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Decode for Decoder<'_>

Source§

impl<R: Read> Decode for IoDecoder<R>

Available on crate feature std only.