pub struct IoDecoder<R: Read> { /* private fields */ }std only.Expand description
Streaming decoder that reads directly from any Read-shaped source.
Each IoDecoder::read call may issue many small reads against the
underlying source. Wrap raw sockets / files in std::io::BufReader
first if read-syscall amplification is a concern.
§Examples
use pack_io::{IoEncoder, IoDecoder};
use std::io::Cursor;
let mut buf: Vec<u8> = Vec::new();
{
let mut enc = IoEncoder::new(&mut buf);
enc.write(&42_u64).unwrap();
enc.write(&"hi").unwrap();
}
let mut dec = IoDecoder::new(Cursor::new(buf));
let n: u64 = dec.read().unwrap();
let s: String = dec.read().unwrap();
assert_eq!((n, s.as_str()), (42, "hi"));Implementations§
Source§impl<R: Read> IoDecoder<R>
impl<R: Read> IoDecoder<R>
Sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Wrap reader with the default Config (1 GiB max_alloc,
depth limit 128).
For input from a peer, use IoDecoder::with_config with
Config::network (or tighter) instead. Whatever the cap, the decoder
only grows a buffer as bytes actually arrive from reader, so a
large declared length on a short or stalled stream does not reserve
memory up front.
§Examples
use std::io::Cursor;
use pack_io::IoDecoder;
let bytes = pack_io::encode(&42_u64).unwrap();
let mut dec = IoDecoder::new(Cursor::new(bytes));
let n: u64 = dec.read().unwrap();
assert_eq!(n, 42);Sourcepub fn with_config(reader: R, config: Config) -> Result<Self>
pub fn with_config(reader: R, config: Config) -> Result<Self>
Wrap reader with the supplied configuration.
§Errors
SerialError::InvalidConfigifconfig.max_alloc == 0,config.max_depth == 0, orconfig.canonicalis set: checking canonical map order needs the raw input, which a streaming decoder does not keep. Read the frame into memory and usecrate::decode_with_config(ordecode_from_with_config) instead.
§Examples
use std::io::Cursor;
use pack_io::{Config, IoDecoder};
let cfg = Config::new().with_max_alloc(16 * 1024);
let bytes = pack_io::encode(&"hello").unwrap();
let mut dec = IoDecoder::with_config(Cursor::new(bytes), cfg).unwrap();
let s: String = dec.read().unwrap();
assert_eq!(s, "hello");Sourcepub fn config(&self) -> Config
pub fn config(&self) -> Config
The configuration this decoder enforces.
§Examples
use std::io::Cursor;
use pack_io::{Config, IoDecoder};
let cfg = Config::new().with_max_alloc(4096);
let dec = IoDecoder::with_config(Cursor::new(Vec::new()), cfg).unwrap();
assert_eq!(dec.config().max_alloc, 4096);Sourcepub fn reader(&self) -> &R
pub fn reader(&self) -> &R
Borrow the underlying reader.
§Examples
use std::io::Cursor;
use pack_io::IoDecoder;
let dec = IoDecoder::new(Cursor::new(vec![0u8; 4]));
assert_eq!(dec.reader().get_ref().len(), 4);Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consume the decoder and return the underlying reader.
Useful when the caller wants to take back ownership of the source (e.g. to drop the reader, return it to a pool, or feed it to a different consumer) after the decoded prefix has been processed.
§Examples
use std::io::Cursor;
use pack_io::IoDecoder;
let bytes = pack_io::encode(&42_u64).unwrap();
let mut dec = IoDecoder::new(Cursor::new(bytes));
let _n: u64 = dec.read().unwrap();
let reader: Cursor<Vec<u8>> = dec.into_inner();
assert_eq!(reader.position(), 1); // one byte consumed for u64=42Sourcepub fn read<T: Deserialize>(&mut self) -> Result<T>
pub fn read<T: Deserialize>(&mut self) -> Result<T>
Decode the next value from the underlying reader.
Each top-level call starts a fresh Config::total_alloc_limit
budget, so a decoder that reads one message after another from a
socket gives every message the same limit.
§Errors
- Propagates any
crate::SerialErrorfrom the type’sDeserialize. - Maps any
std::io::Errorfrom the reader intoSerialError::Io.
Trait Implementations§
Source§impl<R: Read> Decode for IoDecoder<R>
impl<R: Read> Decode for IoDecoder<R>
Source§fn max_alloc(&self) -> usize
fn max_alloc(&self) -> usize
Config::max_alloc.Source§fn depth(&self) -> usize
fn depth(&self) -> usize
0 for decoders that do not track
depth (the default).Source§fn enter_nested(&mut self) -> Result<()>
fn enter_nested(&mut self) -> Result<()>
Source§fn leave_nested(&mut self)
fn leave_nested(&mut self)
enter_nested; call it on the error path
too, so the decoder stays usable after a failed read.Source§fn bytes_read(&self) -> Option<u64>
fn bytes_read(&self) -> Option<u64>
Source§fn unbacked(&self) -> usize
fn unbacked(&self) -> usize
charge_unbacked. Always 0 for decoders
that keep no running total (the default).Source§fn reset_unbacked(&mut self)
fn reset_unbacked(&mut self)
charge_unbacked,
starting the budget of a new top-level value. Read more