pub struct IoEncoder<W: Write> { /* private fields */ }Available on crate feature
std only.Expand description
Streaming encoder that writes directly into any Write-shaped sink.
Each IoEncoder::write call calls into the underlying writer for the
bytes the type produces. The encoder does not buffer; if you wrap a
raw socket / file, wrap it in a std::io::BufWriter first.
§Examples
use pack_io::IoEncoder;
let mut sink: Vec<u8> = Vec::new();
let mut enc = IoEncoder::new(&mut sink);
enc.write(&42_u64).unwrap();
enc.write(&"hello").unwrap();
assert!(!sink.is_empty());Implementations§
Source§impl<W: Write> IoEncoder<W>
impl<W: Write> IoEncoder<W>
Sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
Wrap writer in an encoder.
The encoder does not buffer; wrap raw sockets / files in a
std::io::BufWriter first if syscall amplification is a concern.
§Examples
use pack_io::IoEncoder;
let mut sink: Vec<u8> = Vec::new();
let _enc = IoEncoder::new(&mut sink);Sourcepub fn writer(&self) -> &W
pub fn writer(&self) -> &W
Borrow the underlying writer.
§Examples
use pack_io::IoEncoder;
let mut sink: Vec<u8> = Vec::new();
let enc = IoEncoder::new(&mut sink);
let _: &&mut Vec<u8> = &enc.writer();Sourcepub fn writer_mut(&mut self) -> &mut W
pub fn writer_mut(&mut self) -> &mut W
Borrow the underlying writer mutably.
Useful when downstream code needs &mut W to call writer-specific
methods (e.g. flush) without consuming the encoder.
§Examples
use std::io::{BufWriter, Write};
use pack_io::IoEncoder;
let mut sink: Vec<u8> = Vec::new();
let buffered = BufWriter::new(&mut sink);
let mut enc = IoEncoder::new(buffered);
enc.write(&7_u64).unwrap();
enc.writer_mut().flush().unwrap();Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consume the encoder and return the underlying writer.
§Examples
use pack_io::IoEncoder;
let sink: Vec<u8> = Vec::new();
let mut enc = IoEncoder::new(sink);
enc.write(&42_u64).unwrap();
let written: Vec<u8> = enc.into_inner();
assert_eq!(written, &[0x2a]);Trait Implementations§
Auto Trait Implementations§
impl<W> Freeze for IoEncoder<W>where
W: Freeze,
impl<W> RefUnwindSafe for IoEncoder<W>where
W: RefUnwindSafe,
impl<W> Send for IoEncoder<W>where
W: Send,
impl<W> Sync for IoEncoder<W>where
W: Sync,
impl<W> Unpin for IoEncoder<W>where
W: Unpin,
impl<W> UnsafeUnpin for IoEncoder<W>where
W: UnsafeUnpin,
impl<W> UnwindSafe for IoEncoder<W>where
W: UnwindSafe,
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