-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement is_empty() for BufReader #45369
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
What would code do in response to the emptyness or non-emptyness of the buffer? |
If the buffer is non-empty, then the application might want to read data from it. However, if the buffer is empty the application might not want to issue a potentially expensive (or blocking) call to the underlying Reader just yet. It could also be issued before In my case, I'm using some BufReaders to wrap non-blocking TcpStreams which I poll over using mio. Whenever a message arrives on one of them I read it from the indicated stream. However, to avoid the possible deadlock of polling on the underlying stream for an already buffered message, I must empty the buffer before polling again. But I can't know the buffer is empty without calling read() and having it return zero bytes, which means that I end up doing a bunch of extraneous syscalls just to learn the state of a userspace buffer. |
r? @sfackler (i think alex was traveling and is traveling) |
It is also worth considering if it would make sense to have a |
ping @rust-lang/libs, could one of yinz be a new reviewer on this please? |
Could you expand on this a bit? I don't think I understand it. For example, I don't know why deadlock is a concern here. |
@BurntSushi Consider the following sequence of events:
At this point neither the server nor the client can make forward progress, and are thus deadlocked. In fact, even if the server gets messages from other clients it still won't become unblocked, because it has no reason to check for messages from client A. |
r? @BurntSushi, does @fintelia's comment explain things sufficiently? |
📌 Commit df4b781 has been approved by |
@carols10cents Thanks for the ping. :-) |
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
☀️ Test successful - status-appveyor, status-travis |
@BurntSushi this has |
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