| 1 | use core::pin::Pin; |
| 2 | |
| 3 | use pin_project_lite::pin_project; |
| 4 | |
| 5 | use crate::stream::Stream; |
| 6 | use crate::task::{Context, Poll}; |
| 7 | |
| 8 | pin_project! { |
| 9 | #[derive (Debug)] |
| 10 | pub struct Enumerate<S> { |
| 11 | #[pin] |
| 12 | stream: S, |
| 13 | i: usize, |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | impl<S> Enumerate<S> { |
| 18 | pub(super) fn new(stream: S) -> Self { |
| 19 | Self { stream, i: 0 } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl<S> Stream for Enumerate<S> |
| 24 | where |
| 25 | S: Stream, |
| 26 | { |
| 27 | type Item = (usize, S::Item); |
| 28 | |
| 29 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 30 | let this: Projection<'_, S> = self.project(); |
| 31 | let next: Option<::Item> = futures_core::ready!(this.stream.poll_next(cx)); |
| 32 | |
| 33 | match next { |
| 34 | Some(v: ::Item) => { |
| 35 | let ret: (usize, ::Item) = (*this.i, v); |
| 36 | *this.i += 1; |
| 37 | Poll::Ready(Some(ret)) |
| 38 | } |
| 39 | None => Poll::Ready(None), |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |