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.
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
Maximum number of bytes the decoder will allocate for a single
length-prefixed value. Mirrors
Config::max_alloc.Auto Trait Implementations§
impl<'a> Freeze for Decoder<'a>
impl<'a> RefUnwindSafe for Decoder<'a>
impl<'a> Send for Decoder<'a>
impl<'a> Sync for Decoder<'a>
impl<'a> Unpin for Decoder<'a>
impl<'a> UnsafeUnpin for Decoder<'a>
impl<'a> UnwindSafe for Decoder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more