-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Tracking Issue for BufRead::has_data_left
#86423
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
Comments
Bikeshedding on the name: Better still would be something that make clear what the function is really doing: performing an action, not simply returning some descriptive state flag. Perhaps a name like (I'm afraid I don't know how to rewrite Note: It's possible I've misunderstood the intention behind the proposal. If the goal for a final implementation is to return A second note: Is this the correct place to reply? I'm assuming so because the PR was already merged, but chime in if I'm committing a faux pas. |
TBH when I came up with this I didn't even consider the fact that readers can "spring back to life". The method I'm adding is only meant to check for EOF, rather than the definitive end of the reader (I'm don't think that's even possible for something like a File). You make a good point about the naming. I think something like |
I don't like this I think the correct solution would be to introduce a better API then fn fill_buf2(&mut self) -> Result<Option<&[u8]>> {
let buf = self.fill_buf()?;
if buf.is_empty() {
None
} else {
Some(buf)
}
} This is in my opinion way nicer to use and clearly hint (actually force at compile time) to do think properly. It's also allow to avoid call use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
fn main() -> std::io::Result<()> {
let f = File::open("log.txt")?;
let mut reader = BufReader::new(f);
while let Some(amazing_log_line) = reader.fill_buf2()? {
println!("{:?}", amazing_log_line);
read.consume(amazing_log_line.len());
}
Ok(())
} Random Off topic Idea:
|
The thing with while let Some(_) = reader.fill_buf2()? {
let mut line = String::new();
reader.read_line(&mut line)?;
println!("{:?}", line);
} Since higher-level methods like Your suggestions for coordinating |
that wrong this issue is about let mut line = String::new();
while let Ok(n) = reader.read_line(&mut line)? {
if n == 0 {
break;
}
println!("{:?}", line);
line.clear();
} Or accumulate octets into the line. And once again a better api could be: fn read_line(&mut self, buf: &mut String) -> Result<Option<NonZeroUsize>> { ... } What you propose with |
I think
What is nice about this is
pub trait BufRead2: Read {
fn read_buf(&mut self) -> IoResult<Option<&[u8]>>; // a way to express non empty slice ?
fn consume(&mut self, n: usize) -> Result<???, ???> ;
}
Nevermind, one can do trait BufReadExt: BufRead2 {
fn read_until(&mut self, delimiter: u8, buf: &mut Vec<u8>) -> IoResult<Option<NonZeroUsize>>;
fn read_line(&mut self, buf: &mut String) -> IoResult<Option<NonZeroUsize>>;
fn split(self, delimiter: u8) -> Split<Self>
where
Self: Sized;
fn lines(self) -> Lines<Self>
where
Self: Sized; |
It seems like your suggestion is to add a version of the |
What is the point of adding this API - if this piggy backs on an already exposed API? Anyone can call If the API could figure out before calling |
Without this API you'd need to need to call As for figuring out if a reader has been exhausted without calling |
The problem is we don't know how long is the 'whole data' up front - which is where we need |
In the case of TCP, I don't think what you're proposing is possible. TCP connections terminate by sending a FIN flag, so if you want to know if there's any more data left you'll need to wait for the last packet. |
In that case - |
For files, EOF is reached when the file pointer is at the end of the file and nothing can be read. In that case, |
Feature gate:
#![feature(buf_read_has_data_left)]
This is a tracking issue for adding
has_data_left
toBufRead
. Its purpose is to determine whether a reader has any data left over to read.Example
Steps / History
Unresolved Questions
has_data_left
and whether the API should check the positive case or the negative case.is_at_eof
as the name of the new method).The text was updated successfully, but these errors were encountered: