Skip to content
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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod min_by;
mod next;
mod nth;
mod partial_cmp;
mod peekable;
mod scan;
mod skip;
mod skip_while;
Expand Down Expand Up @@ -80,6 +81,7 @@ pub use filter::Filter;
pub use fuse::Fuse;
pub use inspect::Inspect;
pub use map::Map;
pub use peekable::Peekable;
pub use scan::Scan;
pub use skip::Skip;
pub use skip_while::SkipWhile;
Expand Down Expand Up @@ -974,6 +976,37 @@ extension_trait! {
}
}

#[doc = r#"
## Examples

```
# fn main() { async_std::task::block_on(async {
#
use std::collections::VecDeque;

use async_std::prelude::*;

let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
let mut s = s.peekable();

assert_eq!(s.next().await, Some(1));
assert_eq!(s.peek().await, Some(1));
assert_eq!(s.next().await, Some(2));
assert_eq!(s.next().await, Some(3));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
#[inline]
fn peekable(self) -> Peekable<Self>
where
Self: Sized,
{
Peekable::new(self)
}


#[doc = r#"
A stream adaptor similar to [`fold`] that holds internal state and produces a new
stream.
Expand Down
65 changes: 65 additions & 0 deletions src/stream/stream/peekable.rs
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>>);
Copy link
Contributor

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!)


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, the peeked just should hold the next. And every poll_next, it will be changed to new next. Why do *self.as_mut().peeked() = None;

}
None => {
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match is redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The 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),
}
}
}
}
}