Skip to main content

Writer

Trait Writer 

Source
pub trait Writer {
    // Required method
    fn write(&mut self, bytes: &[u8]);

    // Provided methods
    fn write_byte(&mut self, byte: u8) { ... }
    fn reserve(&mut self, _additional: usize) { ... }
    fn write_with(&mut self, len: usize, fill: impl FnOnce(&mut [u8]) -> usize) { ... }
    fn append_with<T>(
        &mut self,
        len: usize,
        append: impl FnOnce(&mut Vec<u8>) -> T,
    ) -> T { ... }
    fn append_limit(&self) -> usize { ... }
}
Expand description

Byte sink used by every serializer in this crate.

Vec<u8> is the primary implementation; IoWriter adapts any std::io::Write with an internal buffer.

Required Methods§

Source

fn write(&mut self, bytes: &[u8])

Provided Methods§

Source

fn write_byte(&mut self, byte: u8)

Source

fn reserve(&mut self, _additional: usize)

Source

fn write_with(&mut self, len: usize, fill: impl FnOnce(&mut [u8]) -> usize)

Hands the sink a scratch region of len bytes, keeps the first fill(region) bytes of it and discards the rest.

Source

fn append_with<T>( &mut self, len: usize, append: impl FnOnce(&mut Vec<u8>) -> T, ) -> T

Hands append a buffer to append at most len bytes to, passes what it appended on to the sink and returns what append returned.

Sinks backed by a Vec<u8> hand over that vector, so the encoders write their output in place.

Source

fn append_limit(&self) -> usize

Largest len worth passing to Writer::append_with at once; longer output is appended in pieces of at most this size.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Writer for Vec<u8>

Source§

fn write(&mut self, bytes: &[u8])

Source§

fn write_byte(&mut self, byte: u8)

Source§

fn reserve(&mut self, additional: usize)

Source§

fn write_with(&mut self, len: usize, fill: impl FnOnce(&mut [u8]) -> usize)

Source§

fn append_with<T>( &mut self, _len: usize, append: impl FnOnce(&mut Vec<u8>) -> T, ) -> T

Source§

fn append_limit(&self) -> usize

Source§

impl<W: Writer + ?Sized> Writer for &mut W

Source§

fn write(&mut self, bytes: &[u8])

Source§

fn write_byte(&mut self, byte: u8)

Source§

fn reserve(&mut self, additional: usize)

Source§

fn write_with(&mut self, len: usize, fill: impl FnOnce(&mut [u8]) -> usize)

Source§

fn append_with<T>( &mut self, len: usize, append: impl FnOnce(&mut Vec<u8>) -> T, ) -> T

Source§

fn append_limit(&self) -> usize

Implementors§

Source§

impl<W: Write> Writer for IoWriter<W>