-
Notifications
You must be signed in to change notification settings - Fork 628
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
Peek
future for Peekable
stream
#2021
Conversation
a31efb9
to
87be14e
Compare
Changing |
@Nemo157 Is it possible? |
Because of this, The point is to make polling on |
|
@Nemo157 Sorry, backtracking a little here. I just realised that it is still not possible to use the original Say we use the original impl<'a, St> Future for Peek<'a, St>
where
St: Stream,
{
type Output = Option<&'a St::Item>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(mut peekable) = self.inner.take() {
match peekable.as_mut().poll_peek(cx) {
Poll::Pending => {
self.inner = Some(peekable);
Poll::Pending
}
Poll::Ready(poll) => Poll::Ready(poll),
}
} else {
Poll::Pending
}
}
} Now we start to see lifetime issues, because we are borrowing from the
|
There is a bit of tricky borrowing happening here when we stick to the old Returning the mutable borrow back from |
@Nemo157 I have been playing around with |
Yeah, this is actually a really nasty intersection of lifetimes (similar to For backwards compatibility I think you should rename the internal method something like |
Nice! Can you add a test for this? |
5dbab1a
to
f753463
Compare
@cramertj Sure. A simple is added as an illustration of its function. |
LGTM modulo remaining CI failures, which look legit:
|
Thanks! |
Fix #1871
This pull request is an attempt to provide a sensible peeking behaviour for
Peekable
streams. Instead of exposing wires that resemblespoll
fromFuture
, this construction wraps this exposure into a properFuture
, with pinned borrow of the underlyingPeekable
. It works by swapping out the mutable borrow, attempting a poll, then either swapping the mutable borrow back in ifPending
, or producing thePoll
directly.By the construction,
peek()
can be reinstated and the documentation becomes accurate again.