async_std/stream/stream/
map.rs

1use core::pin::Pin;
2
3use pin_project_lite::pin_project;
4
5use crate::stream::Stream;
6use crate::task::{Context, Poll};
7
8pin_project! {
9    /// A stream that maps value of another stream with a function.
10    #[derive(Debug)]
11    pub struct Map<S, F> {
12        #[pin]
13        stream: S,
14        f: F,
15    }
16}
17
18impl<S, F> Map<S, F> {
19    pub(crate) fn new(stream: S, f: F) -> Self {
20        Self {
21            stream,
22            f,
23        }
24    }
25}
26
27impl<S, F, B> Stream for Map<S, F>
28where
29    S: Stream,
30    F: FnMut(S::Item) -> B,
31{
32    type Item = B;
33
34    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
35        let this = self.project();
36        let next = futures_core::ready!(this.stream.poll_next(cx));
37        Poll::Ready(next.map(this.f))
38    }
39}