-
Notifications
You must be signed in to change notification settings - Fork 20
ACP: abstract BufReader::peek
#569
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
I don't think your proposal fully addresses this. It's perfectly valid to implement |
Oh, it does but I forgot to mention how. :) The decoder can check |
We discussed this in the @rust-lang/libs-api meeting today and we are happy to accept this, but with some changes.
Please open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. You can close this ACP once the tracking issue has been created. |
I assuming you meant "change" rather than "can". |
Sounds reasonable
Oh, this is true but non-obvious - no actual allocation in Rust can be above Unfortunately I'm unable to do more than reply to this right now, will do the rest later. |
If I'm interpreting this ACP correctly, then |
maybe rename |
Proposal
Problem statement
Because of how
BufRead::fill_buf
works, chunked decoders such as base64 or hex that decode from generic readers are currently forced to maintain an internal buffer of unread byte chunks. This makes the code much more complex, error prone and slower by introducing additional copies along with all the branches required to handle the edge cases.BufReader::peek
solves this but only for the specific case when the reader isBufReader
, not for generic readers (e.g. slices).Motivating examples or use cases
The
base64
crate has this code:The three additional fields hold an internal buffer that gets copied from the internal reader. This means that reading from e.g. a slice adds an additional copy and complex handling of edge cases. (You can see quite complicated code if you read that file.) The
BufRead
trait almost solves it but one edge case still remains: if the number of bytes in the reader is less than the chunk size the only way to progress is to copy the bytes into an intermediate buffer before continuing with decoding. As mentioned,BufReader::peek
can solve this but storingBufReader
internally would add additional copying if an already-buffered reader is used.Solution sketch
We can solve these problems by adding this trait:
Libraries that implement decoding like base64 can then just bound on
RequireBytes
and avoid all the complexity.Alternatives
Strictly speaking, this doesn't require
std
. Any library wishing to do this can define its own trait and implement it for allstd
types (onceBufReader::peek
is stable). However it would be annoying to coordinate implementations across various crates. Having a "common vocabulary" trait instd
would make the implementation more easily accessible.Links and related work
BufReader::peek
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: