I want to write an efficient event library that reads events from some Read
er, 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.