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