Trait read_collection::BufReadBack

source ·
pub trait BufReadBack: ReadBack {
    // Required methods
    fn read_back_fill_buf(&mut self) -> Result<&[u8]>;
    fn read_back_consume(&mut self, amt: usize);

    // Provided methods
    fn read_back_has_data_left(&mut self) -> Result<bool> { ... }
    fn read_back_until(&mut self, delim: u8, buf: &mut Vec<u8>) -> Result<usize> { ... }
    fn read_back_skip_until(&mut self, delim: u8) -> Result<usize> { ... }
    fn read_back_line(&mut self, dest: &mut String) -> Result<usize> { ... }
    fn read_back_split(self, delim: u8) -> ReadBackSplit<Self> 
       where Self: Sized { ... }
    fn read_back_lines(self) -> RevLines<Self>
       where Self: Sized { ... }
}
Expand description

A BufReadBack is a type of ReadBacker which has an internal buffer, allowing it to perform extra ways of reading.

It behaves the same as BufRead except that it uses ReadBack internally.

Required Methods§

source

fn read_back_fill_buf(&mut self) -> Result<&[u8]>

Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty.

This function is a lower-level call. It needs to be paired with the read_back_consume method to function properly. When calling this method, none of the contents will be “read back” in the sense that later calling read_back may return the same contents. As such, consume must be called with the number of bytes that are consumed from this buffer to ensure that the bytes are never returned twice.

An empty buffer returned indicates that the stream has reached the beginning again.

§Error

This function will return an I/O error if the underlying reader was read, but returned an error.

§Example

TODO

source

fn read_back_consume(&mut self, amt: usize)

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to read_back.

It basically behaves the same as BufRead::consume except that you should combine this with read_back_fill_buf.

Provided Methods§

source

fn read_back_has_data_left(&mut self) -> Result<bool>

Check if the underlying ReadBack has any data left to be read.

This function may fill the buffer to check for data, so this functions returns Result<bool>, not bool.

Default implementation calls read_back_fill_buf and checks that returned slice is empty (which means that there is no data left, since the start is reached).

§Example

TODO

source

fn read_back_until(&mut self, delim: u8, buf: &mut Vec<u8>) -> Result<usize>

Read all bytes into buf until the delimiter byte or the beginning of the reader is reached.

This function will read bytes from the underlying stream until the delimiter or the beginning of the reader is reached. Once found, all bytes up to, and including, the delimiter (if found) will be appended to buf.

If successful, this function will return the total number of bytes read.

§Example

TODO

source

fn read_back_skip_until(&mut self, delim: u8) -> Result<usize>

Skip all bytes until the delimiter byte or the beginning is reached.

This function will read (and discard) bytes from the underlying stream until the delimiter or EOF is found.

If successful, this function will return the total number of bytes read, including the delimiter byte.

This is useful for efficiently skipping data such as NUL-terminated strings in binary file formats without buffering.

§Example
source

fn read_back_line(&mut self, dest: &mut String) -> Result<usize>

Read all bytes until a newline (the 0xA byte) is reached, and prepend them to the provided String buffer.

This function also behaves similar as BufRead::read_line except that it uses the functions of ReadBack instead of Read.

§Example

TODO

source

fn read_back_split(self, delim: u8) -> ReadBackSplit<Self>
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte.

This function also behaves similar as BufRead::split except that it uses the functions of ReadBack instead of Read.

§Example

TODO

source

fn read_back_lines(self) -> RevLines<Self>
where Self: Sized,

Returns an iterator over the lines of this reader.

This function also behaves similar as BufRead::lines except that it uses the functions of ReadBack instead of Read.

§Example

TODO

Implementations on Foreign Types§

source§

impl BufReadBack for &[u8]

source§

fn read_back_consume(&mut self, amt: usize)

Instead of consuming from left to right, we’re consuming the bytes from right to left.

§Example
use read_collection::BufReadBack;

fn main() {
    let data: [u8; 3] = [1, 2, 3];
    let mut reference = data.as_slice();

    reference.read_back_consume(1);
    assert_eq!(reference, &[1, 2]);
}
source§

fn read_back_fill_buf(&mut self) -> Result<&[u8]>

source§

impl BufReadBack for Empty

source§

fn read_back_fill_buf(&mut self) -> Result<&[u8]>

source§

fn read_back_consume(&mut self, _amt: usize)

source§

impl<R: BufReadBack> BufReadBack for &mut R

source§

fn read_back_fill_buf(&mut self) -> Result<&[u8]>

source§

fn read_back_consume(&mut self, amt: usize)

source§

fn read_back_has_data_left(&mut self) -> Result<bool>

source§

fn read_back_until(&mut self, delim: u8, buf: &mut Vec<u8>) -> Result<usize>

source§

fn read_back_skip_until(&mut self, delim: u8) -> Result<usize>

source§

fn read_back_line(&mut self, dest: &mut String) -> Result<usize>

source§

impl<R: BufReadBack> BufReadBack for Box<R>

source§

fn read_back_fill_buf(&mut self) -> Result<&[u8]>

source§

fn read_back_consume(&mut self, amt: usize)

source§

fn read_back_has_data_left(&mut self) -> Result<bool>

source§

fn read_back_until(&mut self, delim: u8, buf: &mut Vec<u8>) -> Result<usize>

source§

fn read_back_skip_until(&mut self, delim: u8) -> Result<usize>

source§

fn read_back_line(&mut self, dest: &mut String) -> Result<usize>

Implementors§