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

BufReader/Writer extension methods: is_empty, buffer #45323

Closed
fintelia opened this issue Oct 16, 2017 · 38 comments · Fixed by #61235
Closed

BufReader/Writer extension methods: is_empty, buffer #45323

fintelia opened this issue Oct 16, 2017 · 38 comments · Fixed by #61235
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@fintelia
Copy link
Contributor

fintelia commented Oct 16, 2017

Update: this is the tracking issue for:

impl<R: Read> BufReader<R> {
    pub fn buffer(&self) -> &[u8] {}

    #[rustc_deprecated(since = "1.26.0", reason = "use .buffer().is_empty() instead")]
    pub fn is_empty(&self) -> bool {}
}

is_empty is both unstable and deprecated, it should be removed.


There is currently no way to tell whether there is any data buffered inside a BufReader. This is unfortunate because it means that an application has no way to know whether calls to read() and fill_buffer() will return instantly or trigger a potentially expensive call to the underlying Reader. I propose adding an is_empty() method to BufReader to fill this gap.

@kennytm kennytm added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 16, 2017
bors added a commit that referenced this issue Nov 6, 2017
Implement is_empty() for BufReader

Simple implementation of `is_empty` method for BufReader so it is possible to tell whether there is any data in its buffer.

I didn't know correct stability annotation to place on the function. Presumably there is no reason to place this feature behind a feature flag, but I wasn't sure how to tag it as an unstable feature without that.

CC: #45323
@jonhoo
Copy link
Contributor

jonhoo commented Nov 6, 2017

@kennytm now that #45369 has been merged, I'm guessing this should get the C-tracking-issue label?

@kennytm kennytm added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 7, 2017
@dbrgn
Copy link
Contributor

dbrgn commented Feb 19, 2018

What's left for this to stabilize?

I'm parsing bytes from a BufReader to MsgPack using read_value. The function will stop parsing once a valid MsgPack value has been found. But in my application the parsing should fail if the buffer hasn't been consumed completely. Checking buf.is_empty() would probably be the cleanest way to do so.

@jonhoo
Copy link
Contributor

jonhoo commented Feb 19, 2018

@dbrgn the BufReader::buf_bytes method proposed in #48022 may also be useful for that purpose, though that may take even longer to land.

@SimonSapin
Copy link
Contributor

This is the tracking issue for:

impl<R: Read> BufReader<R> {
    pub fn is_empty(&self) -> bool { /*…*/ }
}

Looks good to me to stabilize.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Mar 17, 2018

Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Mar 17, 2018
@sfackler
Copy link
Member

This seems fine to stabilize, but it seems like it'd be more useful to know how many bytes are buffered, rather than just that there are some number of bytes.

@SimonSapin
Copy link
Contributor

Why not both? Slices have is_empty even though it’s somewhat redundant with len.

@sfackler
Copy link
Member

For sure. Would it be reasonable to try to squeeze that into this stabilization?

@SimonSapin
Copy link
Contributor

Sounds ok to me, though we should probably (re)start FCP after the implementation is merged.

@sfackler
Copy link
Member

Naming wise, I don't think len fits since that seems to me like it'd refer to the length of the entire reader rather than just the current buffered data (similarly is_empty is a bit confusing). How about buffered_len and buffer_is_empty?

@SimonSapin
Copy link
Contributor

Agreed. Sounds good to me.

@sfackler
Copy link
Member

Alternatively, we could add fn buffer(&self) -> &[u8] to just return the buffer, which is also a thing people want separately from this issue. At that point r.buffer().len() seems not much worse than r.buffer_len() so we could have one method instead of three.

@sfackler
Copy link
Member

#49139 adds buffer and deprecates is_empty.

@rfcbot
Copy link

rfcbot commented Mar 19, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Mar 19, 2018
@fintelia
Copy link
Contributor Author

With @sfackler's alternative (and IMO better) proposal just introduced, should this still be going into its final comment period?

@SimonSapin
Copy link
Contributor

@rfcbot fcp cancel

Let’s start again after buffer() has landed.

@rfcbot
Copy link

rfcbot commented Mar 19, 2018

@SimonSapin proposal cancelled.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Mar 19, 2018
@jonhoo
Copy link
Contributor

jonhoo commented May 30, 2018

We should likely also add a .buffer() method to BufWriter for consistency?

@sfackler
Copy link
Member

That can't be done backwards compatibly.

@jonhoo
Copy link
Contributor

jonhoo commented May 30, 2018

@sfackler why not? It's just a struct, right? Just like BufReader.

@Mark-Simulacrum Mark-Simulacrum changed the title BufReader should have an is_empty() method BufReader/Writer extension methods: is_empty, buffer Jun 2, 2018
@fintelia
Copy link
Contributor Author

It has been a while since buffer landed. Is this ready to stabilize?

@SimonSapin
Copy link
Contributor

BufReader::buffer is implemented, but BufWriter::buffer discussed above is not. (I’ve updated the issue’s description with the current status.)

@fintelia
Copy link
Contributor Author

Wait, we aren't proposing to stabilize the (already deprecated) is_empty method are we?

@SimonSapin
Copy link
Contributor

No, sorry I didn’t clarify. An API marked both unstable and deprecated is to be removed (after something like a couple release cycles of deprecation period, which we’re at in this case).

@zslayton
Copy link
Contributor

I'm excited to see that this is so close to landing. Is this still waiting on an implementation of BufWriter::buffer? Would it be possible to track that feature separately to unblock this one?

kennytm added a commit to kennytm/rust that referenced this issue Nov 14, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this issue Nov 15, 2018
@fintelia
Copy link
Contributor Author

Bump. Is there anything else blocking stabilization?

@edef1c
Copy link
Contributor

edef1c commented Mar 11, 2019

Is there any particular reason not to add fn buffer(&self) -> &[u8] { &[] } to BufWrite?
It seems like a useful enough general-purpose API ("get me however many bytes you can easily give me"), and I found myself looking for such a method while working on parser tooling.
Individual implementations could make stricter guarantees about the lower bounds, of which "at least the unconsumed fraction of the last fill_buf" would be a common one.

@fintelia
Copy link
Contributor Author

fintelia commented Mar 12, 2019

Yeah, this has been a while. @SimonSapin @sfackler is this ready for a vote to stabilize?

@fintelia
Copy link
Contributor Author

This issue seems to have been stuck in a strange limbo state for quite a while now. Pinging more people the rfcbot mentioned... @alexcrichton @aturon @dtolnay @withoutboats

@SimonSapin
Copy link
Contributor

Sorry we dropped the ball here. This is now one method on the BufReader and BufWriter structs.

impl<R> BufReader<R> {
    pub fn buffer(&self) -> &[u8] {}
}
impl<W: Write> BufWriter<W> {
    pub fn buffer(&self) -> &[u8] {}
}

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented May 10, 2019

Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 10, 2019
@SimonSapin
Copy link
Contributor

@edef1c There is nothing called BufWrite in std. Did you mean the BufRead trait? A downside is that to add it now we would have to make it a default method, which would (at first) have an inconsistent behavior with fill_buf for existing impls. We can update impls in std at the same time, but that leaves impls in other crates.

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label May 15, 2019
@rfcbot
Copy link

rfcbot commented May 15, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label May 15, 2019
@edef1c
Copy link
Contributor

edef1c commented May 25, 2019

@SimonSapin Sorry, yes, I meant BufRead. For my use cases, not having a lower bound is fine. Anything that does can use a marker subtrait that encompasses that guarantee.
I think the only stipulation should be that it not provide more than fill_buf, but less is fine. For example, an implementation might copy existing data around on fill_buf to make a larger contiguous segment, but can't/shouldn't do so in buffer (I think it should be a cheap conversion, as_buffer wouldn't have been an unreasonable name).

@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label May 25, 2019
@rfcbot
Copy link

rfcbot commented May 25, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label May 25, 2019
Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants