-
Notifications
You must be signed in to change notification settings - Fork 339
adds stream::enumerate combinator #178
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
Changes from 5 commits
ed944d0
2d75ffa
cdd4215
9b36582
3dc33f5
9487b73
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,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 + Unpin + Sized, | ||
{ | ||
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), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
mod all; | ||
mod any; | ||
mod enumerate; | ||
mod filter_map; | ||
mod find; | ||
mod find_map; | ||
|
@@ -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; | ||
|
@@ -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); | ||
/// | ||
/// # | ||
/// # }) } | ||
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. This is a great test! But I feel like the example from 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 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. yep, makes sense |
||
fn enumerate(self) -> Enumerate<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
Enumerate::new(self) | ||
} | ||
|
||
/// Both filters and maps a stream. | ||
/// | ||
/// # Examples | ||
|
Uh oh!
There was an error while loading. Please reload this page.