-
Notifications
You must be signed in to change notification settings - Fork 341
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
add stream::peekable #366
base: main
Are you sure you want to change the base?
add stream::peekable #366
Changes from 5 commits
8f2ed48
4f4a017
a2dc753
d644eee
0aef382
bd104c2
dc04fde
f37916c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::pin::Pin; | ||
|
||
use crate::future::Future; | ||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct Peekable<S: Stream> | ||
where | ||
S: Sized, | ||
{ | ||
stream: S, | ||
peeked: Option<Option<S::Item>>, | ||
} | ||
|
||
//pub struct PeekFuture<'a, T: Unpin + ?Sized> { | ||
//pub(crate) stream: &'a T, | ||
//} | ||
|
||
//impl<T: Stream + Unpin + ?Sized> Future for PeekFuture<'_, T> { | ||
//} | ||
|
||
impl<S> Peekable<S> | ||
where | ||
S: Stream, | ||
{ | ||
pin_utils::unsafe_pinned!(stream: S); | ||
pin_utils::unsafe_unpinned!(peeked: Option<Option<S::Item>>); | ||
|
||
pub(crate) fn new(stream: S) -> Self { | ||
Peekable { | ||
stream: stream, | ||
peeked: None, | ||
} | ||
} | ||
|
||
pub fn peek(&mut self) -> impl Future<Output = Option<S::Item>> { | ||
async { None } | ||
} | ||
} | ||
|
||
impl<S> Stream for Peekable<S> | ||
where | ||
S: Stream, | ||
{ | ||
type Item = S::Item; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
match &self.peeked { | ||
Some(_) => { | ||
let v = Poll::Ready(self.as_mut().peeked().take().unwrap()); | ||
*self.as_mut().peeked() = None; | ||
v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, the |
||
} | ||
None => { | ||
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx)); | ||
match next { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Poll::Ready(next) |
||
Some(v) => Poll::Ready(Some(v)), | ||
None => Poll::Ready(None), | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice if this could be updated to use
pin-project-lite
the way we've done for other combinators (we merged this in the last week; in general it should be nicer!)