Skip to content

Commit

Permalink
Merge #178
Browse files Browse the repository at this point in the history
178: adds stream::enumerate combinator r=stjepang a=montekki

enumerate might be handy.
---
Stdlib: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.enumerate
Ref: #129 

Co-authored-by: Fedor Sakharov <[email protected]>
  • Loading branch information
bors[bot] and montekki authored Sep 17, 2019
2 parents d73e544 + 9487b73 commit 08d954b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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 @@ -39,6 +40,7 @@ pub use zip::Zip;

use all::AllFuture;
use any::AnyFuture;
use enumerate::Enumerate;
use filter_map::FilterMap;
use find::FindFuture;
use find_map::FindMapFuture;
Expand Down Expand Up @@ -197,6 +199,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);
///
/// #
/// # }) }
fn enumerate(self) -> Enumerate<Self>
where
Self: Sized,
{
Enumerate::new(self)
}

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

0 comments on commit 08d954b

Please sign in to comment.