-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters