-
Notifications
You must be signed in to change notification settings - Fork 19
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
ACP: BufReader::peek #417
Comments
This doesn't differ much from |
using (sidenote: when looking for a function to compare it to i was surprised that rust doesn't appear to have an equivalent to Go's |
So, backshifting? Yeah, people have asked for this. Though I think a backshift-and-read-once-more function might be a possible middle ground. If the read doesn't read enough you can call it again without having to fill the entire buffer. |
The other obvious
None of the BufReader methods resizes the buffer. The buffer is actually now implemented as a |
It used to be a vec. So that's more of an implementation detail and could be changed. But then we should have explicit resize methods. Introducing it through implicit resizing would be odd. |
I think the
Step 1 is considering that, if the buffer is large enough to support peeking Step 2 may refill the buffer up to |
yep i looked at the implementation, that's why this is an alternative and not the main proposal
seems like it handles that one edge case a lot better with no real downside, nice! |
We discussed this during today's libs-API meeting. We're generally ok with adding this method, but the tracking issue should note the open questions whether requesting an Speaking for myself, I think we should also have lower level building blocks |
I first posted that in the tracking issue, but I think here is a better place. What I'm quoting below is the question in that tracking issue.
Another possibility that may be more in line with other methods reading more than one item, like impl BufReader {
fn peek(&mut self, n: usize, buf: &mut [u8]) -> io::Result<usize>
} I'm suggesting a It could also be convenient to have it as a trait rather than a implementation of only |
@blueglyph you seem to be suggesting using alternatives #1 and #2 together. |
@lolbinarycat Heh, you're right. I first saw the tracking issue and the questions I quoted, without the alternatives. I replied there, then I saw there was a proposal for it here, quickly moved the suggestion and misread the alternatives in my hurry. Sorry. Just ignore my earlier comment. FWIW, I also think returning a shorter slice instead of an error is more useful and perhaps more conventional. Errors should be reserved for unexpected I/O issues, but when someone peeks further without knowing the total size, it's hardly an error to give PS: I had also thought of the |
with a function like with a function like the proposed |
That's a good point. The length is returned explicitly, but ignoring it could be undetected. And maybe it's easier to ignore it when Note that you can use the |
Proposal
Problem statement
It is often desirable to look ahead a small amount in an unseekable stream, such as a unix pipe.
Motivating examples or use cases
the main usecase is inspecting the magic number of a file, such as is trying to be done here, but it could also be useful for some simple parsers.
Solution sketch
add a method
BufReader::peek
that has the signaturefn peek(&mut self, n: usize) -> io::Result<&[u8]>
the method would work as followed:
read
calls on the underlying Read object to fill the buffer until it reaches any of: the length requested, the BufReader'scapacity
, or end of fileAlternatives
Read::read
(adds an extra copy, but has nice parity)Peek
trait that abstracts over all streams that can do a limited lookaheadLinks and related work
original thread linked previously
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: