pub struct Decoder<'a> { /* private fields */ }Expand description
In-memory decoder. Borrows from an input slice and advances a position pointer as values are read. Bounds-checked on every operation.
Implements Decode, so Deserialize impls written generically over
D: Decode work directly through it.
§Examples
use pack_io::{Encoder, Decoder};
let mut enc = Encoder::new();
enc.write(&7_u64).unwrap();
enc.write(&true).unwrap();
let bytes = enc.into_inner();
let mut dec = Decoder::new(&bytes);
let n: u64 = dec.read().unwrap();
let b: bool = dec.read().unwrap();
assert_eq!(n, 7);
assert!(b);
assert!(dec.is_empty());Implementations§
Source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
Sourcepub fn with_config(bytes: &'a [u8], config: Config) -> Result<Self>
pub fn with_config(bytes: &'a [u8], config: Config) -> Result<Self>
Construct a decoder with the supplied configuration.
§Errors
Returns SerialError::InvalidLength if config.max_alloc == 0.
Sourcepub fn read<T: Deserialize>(&mut self) -> Result<T>
pub fn read<T: Deserialize>(&mut self) -> Result<T>
Decode a value of type T from the current position.
§Errors
Returns any SerialError surfaced by T::deserialize.
Sourcepub fn read_length_prefixed_borrowed(&mut self) -> Result<&'a [u8]>
pub fn read_length_prefixed_borrowed(&mut self) -> Result<&'a [u8]>
Read a length-prefixed byte run as a borrowed slice of the underlying input — no allocation, no copy.
The borrowed slice has the same lifetime 'a as the decoder’s
input buffer, which lets caller-side &'a str / &'a [u8] decode
paths return a borrow directly into that buffer. This is the seam
the zero-copy crate::DeserializeView surface plugs into for
&'a str and &'a [u8].
§Errors
SerialError::InvalidLengthif the prefix exceeds the configuredmax_alloc, OR exceeds the remaining input.SerialError::UnexpectedEofis folded intoInvalidLengthfor this method, since the buffer length is known up front and a declared length running off the end is logically a length-prefix error, not a streaming EOF.
Trait Implementations§
Source§impl Decode for Decoder<'_>
impl Decode for Decoder<'_>
Source§fn read_length_prefixed(&mut self) -> Result<Vec<u8>>
fn read_length_prefixed(&mut self) -> Result<Vec<u8>>
In-memory specialisation: validates length against the actual buffer
length too, not just max_alloc. Catches truncated inputs without
allocating.
Source§fn max_alloc(&self) -> usize
fn max_alloc(&self) -> usize
Config::max_alloc.