Skip to main content

Crate leftover

Crate leftover 

Source
Expand description

Buffered reading with support for data that spans chunks.

LeftoverReader wraps a BufRead reader and provides fill_view to access saved bytes alongside the current chunk. If more input is needed, accumulate copies the whole chunk into the leftover for the next read.

A LeftoverView lets you inspect, take, or discard these bytes.

§Example

Collect newline-delimited records, keeping the delimiters. A small buffer makes records span chunks; take_owned copies each record into a new vector so it can be kept after the next read.

use std::io::BufReader;

use leftover::LeftoverReader;

let input = BufReader::with_capacity(3, b"one\n\nlast".as_slice());
let mut reader = LeftoverReader::new(input);
let mut records = Vec::new();

loop {
    let view = reader.fill_view()?;
    if view.buffer().is_empty() {
        // Keep the final record even if it has no newline.
        if !view.leftover().is_empty() {
            records.push(view.take_owned(0));
        }
        break;
    }
    if let Some(end) = view.buffer().iter().position(|&byte| byte == b'\n') {
        // Include the newline in the record.
        records.push(view.take_owned(end + 1));
    } else {
        // Save this part and continue with the next chunk.
        view.accumulate();
    }
}

assert_eq!(records, [b"one\n".as_slice(), b"\n", b"last"]);

Use take_view when you can finish processing the data before filling the next view. consume discards data without copying it.

Structs§

LeftoverReader
A BufRead adapter for processing data across buffer boundaries.
LeftoverView
A view of the saved bytes and the current input chunk.

Functions§

read_until
Read from reader through delim or EOF, appending the bytes to buf.