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

BufRead::is_eof #40747

Closed
wants to merge 1 commit into from
Closed

BufRead::is_eof #40747

wants to merge 1 commit into from

Conversation

stepancheg
Copy link
Contributor

Closes #40745

@rust-highfive
Copy link
Collaborator

r? @brson

(rust_highfive has picked a reviewer for you, use r? to override)

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 22, 2017
@aturon
Copy link
Member

aturon commented Mar 28, 2017

Can you elaborate on the benefits of this function? Seems reasonable, but the libs team is having a hard time imagining where you'd want it.

@stepancheg
Copy link
Contributor Author

Suppose you have a function which reads a message from a stream:

fn read_one_message(stream: &mut BufRead) -> io::Result<Message> { ... }

And you want to read all the messages from file. If you have is_eof, code could be written like this:

fn read_all_messages(stream: &mut BufRead) -> io::Result<Vec<Message>> {
    let mut r = Vec::new();
    while !stream.is_eof()? {
        r.push(read_one_message(stream)?);
    }
    r
}

read_one_message could return io::Result<Option<Message>> though to use None as signal that EOF is reached.

@alexcrichton
Copy link
Member

@stepancheg why would this want to be a trait method though?

The use case there makes sense, but I'm not sure why that means we should add it to the trait.

@stepancheg
Copy link
Contributor Author

I don't understand. Where else should it be added? Usually only buffered readers can tell if EOF is reached.

If you think function is not that useful, I'm OK with just closing the PR.

@sfackler
Copy link
Member

sfackler commented Apr 2, 2017

Implementation-wise, this does differ from similar APIs like feof(3), which only reports EOF after that has actually been "reached" through a read.

@alexcrichton
Copy link
Member

This was discussed at libs triage recently and the conclusion we reached were:

  • The definition of "eof" here is pretty different than other languages' EOF definition (as pointed out by @sfackler)
  • This is relatively easy to implement locally (e.g. fill_buf().is_empty())
  • It's somewhat semantically odd that this is available only on BufRead, not on Read (which some might expect).

Would it be possible to perhaps rename this method to a more evocative name of what it's doing? The conclusion was that we do not want to land this as is_eof but were open to other names.

@Mark-Simulacrum Mark-Simulacrum added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 14, 2017
@shepmaster
Copy link
Member

@stepancheg thank you for the contribution! There's been a suggestion to rename the method, do you think you'll be able to make that change?

@shepmaster
Copy link
Member

@stepancheg thank you for the PR but we are going to close this for now since we haven't heard from you in a few weeks. If you have a chance to update this, please feel free to re-open the PR (or ping us if needed).

@shepmaster shepmaster closed this Apr 28, 2017
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 18, 2021
Add has_data_left() to BufRead

This is a continuation of rust-lang#40747 and also addresses rust-lang#40745. The problem with the previous PR was that it had "eof" in its method name. This PR uses a more descriptive method name, but I'm open to changing it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants