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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
66 changes: 66 additions & 0 deletions src/stream/stream/peekable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::pin::Pin;

use crate::future::Future;
use crate::stream::Stream;
use crate::task::{Context, Poll};
use crate::stream::stream::StreamExt;

#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct Peekable<S: Stream>
where
S: Sized,
{
stream: S,
peeked: Option<Poll<Option<S::Item>>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is what I landed on as the value for peeked

}

impl<S> Peekable<S>
where
S: Stream,
{
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(peeked: Option<Poll<Option<S::Item>>>);

pub(crate) fn new(stream: S) -> Self {
Peekable {
stream: stream,
peeked: None,
}
}

pub fn peek(mut self: Pin<&mut Self>) -> &Poll<Option<S::Item>> {
match &self.peeked {
Some(peeked) => &peeked,
None => {
// how to get the next `next` value? What about `Context`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is where I currently have troubles @yoshuawuyts , I have no idea how to get the next next value from here. Given that I store a Option<Poll<Option<S::Item>>> in the struct.

let next = self.stream.next();
*self.as_mut().peeked() = Some(next);
&next
}
}
}
}

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>> {
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));

match &self.peeked {
Some(peeked) => {
let v = 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 => {
Poll::Ready(next)
}
}
}
}