-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add read_into_buf and get_buf to BufRead #1015
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
- Feature Name: bufread_buf | ||
- Start Date: 2015-03-25 | ||
- RFC PR: | ||
- Rust Issue: | ||
|
||
# Summary | ||
|
||
Add two methods to BufRead, `read_into_buf` and `get_buf`. These would allow filling | ||
up more of the buffer, and inspecting the buffered data without causing an implicit | ||
read. | ||
|
||
# Motivation | ||
|
||
Currently, you can only view the buffer via `fill_buf`. Once the buffer has even a | ||
single byte in it, `fill_buf` no longer fills up more of the buffer. It also causes | ||
a read implicitly if the buffer is empty, even if you only wanted to inspect the | ||
buffer. | ||
|
||
A use case is that of wanting to read more and more into the buffer without consuming | ||
the read data. This could be because one requires a certain amount of bytes to be able | ||
to determine what kind of input was received, and until that is determined, one doesn't | ||
want to throw away the bytes. | ||
|
||
A possible example: | ||
|
||
```rust | ||
loop { | ||
match try!(rdr.read_into_buf()) { | ||
0 => return Err(Incomplete), | ||
_ => { | ||
if is_foo(rdr.get_buf()) { | ||
rdr.consume(FOO_BYTES); | ||
return Ok(Foo) | ||
} | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
# Detailed design | ||
|
||
Add the following methods to `BufRead`: | ||
|
||
```rust | ||
trait BufRead: Read { | ||
// ... | ||
|
||
/// This will read from the inner Read, and append the data onto the end of | ||
/// any currently buffered data. | ||
fn read_into_buf(&mut self) -> io::Result<usize>; | ||
|
||
/// Get a read-only view into the buffered data. | ||
/// | ||
/// NOTE: This method **will not** do an implicit read if the buffer is empty. | ||
/// It will just return an empty slice. | ||
fn get_buf(&self) -> &[u8]; | ||
|
||
} | ||
|
||
``` | ||
|
||
# Drawbacks | ||
|
||
Adds additional methods that a `BufRead` implementation must include. | ||
|
||
# Alternatives | ||
|
||
An alternative to this is to not add the `read_into_buf` method, and instead to | ||
change `fill_buf` to act just like it. This may be more intuitive, or not, depending | ||
on your assumptions. | ||
|
||
# Unresolved questions | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value is... how much data is currently in the buffer, or how much was read by just this call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns how much was read by this call. You can check the size of the current buffer with
get_buf().len()
.