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

adds stream::enumerate combinator #178

Merged
merged 6 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions src/stream/stream/enumerate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::task::{Context, Poll};
use std::pin::Pin;

use crate::stream::Stream;

#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct Enumerate<S> {
stream: S,
i: usize,
}

impl<S> Enumerate<S> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(i: usize);

pub(super) fn new(stream: S) -> Self {
Enumerate { stream, i: 0 }
}
}

impl<S> futures_core::stream::Stream for Enumerate<S>
where
S: Stream,
{
type Item = (usize, 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 next {
Some(v) => {
let ret = (self.i, v);
*self.as_mut().i() += 1;
Poll::Ready(Some(ret))
}
None => Poll::Ready(None),
}
}
}
32 changes: 32 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

mod all;
mod any;
mod enumerate;
mod filter_map;
mod find;
mod find_map;
Expand All @@ -35,6 +36,7 @@ pub use take::Take;

use all::AllFuture;
use any::AnyFuture;
use enumerate::Enumerate;
use filter_map::FilterMap;
use find::FindFuture;
use find_map::FindMapFuture;
Expand Down Expand Up @@ -193,6 +195,36 @@ pub trait Stream {
}
}

/// Creates a stream that gives the current element's count as well as the next value.
///
/// # Overflow behaviour.
///
/// This combinator does no guarding against overflows.
///
/// # Examples
/// ```
/// # fn main() { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use std::collections::VecDeque;
///
/// let s: VecDeque<_> = vec!['a', 'b', 'c'].into_iter().collect();
/// let mut s = s.enumerate();
///
/// assert_eq!(s.next().await, Some((0, 'a')));
/// assert_eq!(s.next().await, Some((1, 'b')));
/// assert_eq!(s.next().await, Some((2, 'c')));
/// assert_eq!(s.next().await, None);
///
/// #
/// # }) }
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a great test! But I feel like the example from Iterator::enumerate was a little clearer in its intent:

let a = ['a', 'b', 'c'];

let mut iter = a.iter().enumerate();

assert_eq!(iter.next(), Some((0, &'a')));
assert_eq!(iter.next(), Some((1, &'b')));
assert_eq!(iter.next(), Some((2, &'c')));
assert_eq!(iter.next(), None);

Perhaps manually calling next().await might make it easier to follow?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, makes sense

fn enumerate(self) -> Enumerate<Self>
where
Self: Sized,
{
Enumerate::new(self)
}

/// Both filters and maps a stream.
///
/// # Examples
Expand Down