Is there a version of `BufReader` that resizes if the buffer is too small

I want to write an efficient event library that reads events from some Reader, where I may need to backtrack and so I need to be in control of an internal buffer. To make a nice library I want to use a trait like this

pub trait Readable {
    type Error;

    fn read<R>(mut reader: &mut R) -> Result<Self, Self::Error>
    where
        R: io::BufRead // or something better
}

Implementers of this trait should try to read an event, consume the relevant input if they succeed, or consume nothing and return an error if not. My problem is what to do if when I can fill_buf, I don't have enough data to work with.

Solutions I can think of are copying into an internal buffer, but then if I end up reporting an error I may have consumed data from the stream, and so cannot backtrack.

Anyone got any ideas, or can anyone signpost me to relevant topics/reading? What I want really is a version of BufReader where the internal buffer reallocates like Vec when it's full and fill_buf is called.

I'm not sure I'm understanding the problem – is this something like a streaming parser? If you don't have enough data to read, why wouldn't just attempt to read more until you have? You know BufReader's buffer is just an optimization, right? You can read a higher amount data than what fits into its buffer, e.g. using read_exact() or read_to_end().

You may be looking for something like Buffered Reader which offers a lot more control over its internal buffer than the standard BufReader.

If you're interested, the README of the Buffered Reader crate describes the problem I'm trying to solve :slight_smile:.