Skip to main content

Sink

Trait Sink 

Source
pub trait Sink {
    // Required method
    fn put(&mut self, bytes: &[u8]) -> Result<(), Full>;

    // Provided method
    fn remaining(&self) -> Option<usize> { ... }
}
Expand description

The output abstraction every writer takes.

put appends all of bytes or fails with Full. remaining reports how many more bytes fit, when the sink knows; writers that produce a value in several put calls check it first, so a sink that knows its room never holds half a value after an error.

§Examples

use parse_net::{Full, Sink};

/// A sink that keeps only the last byte it was given.
struct Last(Option<u8>);
impl Sink for Last {
    fn put(&mut self, bytes: &[u8]) -> Result<(), Full> {
        if let Some(&b) = bytes.last() {
            self.0 = Some(b);
        }
        Ok(())
    }
}

let mut last = Last(None);
parse_net::write::write_token(b"gzip", &mut last)?;
assert_eq!(last.0, Some(b'p'));

Required Methods§

Source

fn put(&mut self, bytes: &[u8]) -> Result<(), Full>

Appends all of bytes, or fails with Full and appends nothing.

§Errors

Full when the sink has no room for bytes.

Provided Methods§

Source

fn remaining(&self) -> Option<usize>

How many more bytes fit, when the sink knows; None otherwise.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Sink for Vec<u8>

Available on crate feature alloc only.
Source§

fn put(&mut self, bytes: &[u8]) -> Result<(), Full>

Source§

impl<S: Sink + ?Sized> Sink for &mut S

Source§

fn put(&mut self, bytes: &[u8]) -> Result<(), Full>

Source§

fn remaining(&self) -> Option<usize>

Implementors§