I'm writing a streaming binary encoding parser, pulling bytes from a data source that implements the Read
trait. Values in the stream are prefixed with a short header that indicates what type the next value is and how many bytes it takes to represent it.
I've been using Read::bytes
to read the header. Then, if I'm interested in the value that the header represents, I can use the Read::read
method to copy the required number of bytes from the data source into a buffer. However, if I'm not interested in the value, I'd like to skip over those bytes without having to process them. I can achieve this by doing:
for byte_result in data_source.bytes().take(n) {
let _byte = byte_result?;
}
However, checking on the result of each individual byte feels very expensive. Is there a way I can achieve this that only requires that I check the result of an overall skip
operation?
The Seek
trait is promising, but many forms of input do not implement it (TcpStream
, for example.)