Files
async_std
collections
fs
future
io
net
option
os
path
process
result
stream
string
sync
task
unit
vec
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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> 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),
        }
    }
}