Description
Would you like to fix this bug? If so, here's how.
The documentation currently states that BufRead::fill_buf
:
Fills the internal buffer of this object, returning the buffer contents.
However, this is not what BufReader
's implementation of that method does. It will only fill the buffer if no data is currently buffered. If data is already present in the buffer, no filling takes place:
impl<R: Read> BufRead for BufReader<R> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
if self.pos >= self.cap {
self.cap = self.inner.read(&mut self.buf)?;
self.pos = 0;
}
Ok(&self.buf[self.pos..self.cap])
}
}
I would argue that the current behavior of BufReader
's implementation is usually what users would want to do, but if does not match the documentation of, nor the name of, fill_buf
. I don't know what the right solution here is, but I suspect the only safe thing to do would be to update BufReader
's implementation to match the docs (i.e., to make it actually fill its buffer), and then to add another method to BufRead
(perhaps buf_bytes
?) that gives the currently buffered contents, regardless of the amount of buffered data, and without reading from the underlying Read
.