rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.91k stars 1.57k forks source link

Add a flush method to BufRead #993

Open mahkoh opened 9 years ago

mahkoh commented 9 years ago

There is currently no way to discard the buffered bytes.

reem commented 9 years ago

If we do land this I think it should be called something other than flush because flush is not usually associated with losing or getting rid of data.

Gankra commented 9 years ago

Seems like this is analogous to clear on collections

lilyball commented 9 years ago

What's the use-case for discarding the buffered bytes without discarding the actual buffer itself?

seanmonstar commented 9 years ago

A use case would be that after a read that fills the buffer, I can tell a few bytes in that a Request is invalid, and I'd rather use the buffer for a new request, instead of having to allocate again.

On Tue, Mar 24, 2015, 11:25 AM Kevin Ballard notifications@github.com wrote:

What's the use-case for discarding the buffered bytes without discarding the actual buffer itself?

— Reply to this email directly or view it on GitHub https://github.com/rust-lang/rfcs/issues/993#issuecomment-85631671.

lilyball commented 9 years ago

This behavior doesn't make sense for std::io::BufReader, as that wraps another Read and therefore can't be used for a separate Request (as Read is stream-based, not message-based, so you can't know that the buffer only contains data from a single Request even if you're using a message-oriented protocol transported over a stream transport like TCP).

For the other implementations of BufRead:

Ultimately, I think that this functionality should be provided on individual types as desired, rather than on BufRead, because it's just not useful for the general category of buffered stream readers (which is to say, there will never be generic code working on arbitrary BufRead impls that wants this).