Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for BufRead is confusing #85394

Closed
ouromoros opened this issue May 17, 2021 · 0 comments · Fixed by #136177
Closed

Documentation for BufRead is confusing #85394

ouromoros opened this issue May 17, 2021 · 0 comments · Fixed by #136177
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@ouromoros
Copy link

rust/library/std/src/io/mod.rs

Lines 1886 to 1926 in a55748f

#[stable(feature = "rust1", since = "1.0.0")]
pub trait BufRead: Read {
/// Returns the contents of the internal buffer, filling it with more data
/// from the inner reader if it is empty.
///
/// This function is a lower-level call. It needs to be paired with the
/// [`consume`] method to function properly. When calling this
/// method, none of the contents will be "read" in the sense that later
/// calling `read` may return the same contents. As such, [`consume`] must
/// be called with the number of bytes that are consumed from this buffer to
/// ensure that the bytes are never returned twice.
///
/// [`consume`]: BufRead::consume
///
/// An empty buffer returned indicates that the stream has reached EOF.
///
/// # Errors
///
/// This function will return an I/O error if the underlying reader was
/// read, but returned an error.
///
/// # Examples
///
/// A locked standard input implements `BufRead`:
///
/// ```no_run
/// use std::io;
/// use std::io::prelude::*;
///
/// let stdin = io::stdin();
/// let mut stdin = stdin.lock();
///
/// let buffer = stdin.fill_buf().unwrap();
///
/// // work with buffer
/// println!("{:?}", buffer);
///
/// // ensure the bytes we worked with aren't returned again later
/// let length = buffer.len();
/// stdin.consume(length);
/// ```

The doc states thatfill_buf() needs to be paired with consume() to function properly, which can be comprehended to imply that if consume() is not called immediately after fill_buf() it would cause undefined behavior.

Also, it is said that later calling read may return the same contents. It's confusing that may is used instead of will here, and this confuses the reader to a deeper level.

@Dylan-DPC Dylan-DPC added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 13, 2023
hkBst added a commit to hkBst/rust that referenced this issue Jan 28, 2025
Sakib25800 added a commit to Sakib25800/rust that referenced this issue Mar 19, 2025
clarify BufRead::{fill_buf, consume} docs

Fixes rust-lang#85394
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 19, 2025
clarify BufRead::{fill_buf, consume} docs

Fixes rust-lang#85394
@bors bors closed this as completed in 51c5700 Mar 19, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 19, 2025
Rollup merge of rust-lang#136177 - hkBst:patch-24, r=ibraheemdev

clarify BufRead::{fill_buf, consume} docs

Fixes rust-lang#85394
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants