Skip to content

Discrepancy between docs and implementation of BufReader::fill_buf #48022

Closed
@jonhoo

Description

@jonhoo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions