async_std/stream/stream/
find_map.rs

1use core::future::Future;
2use core::pin::Pin;
3use core::task::{Context, Poll};
4
5use crate::stream::Stream;
6
7#[doc(hidden)]
8#[allow(missing_debug_implementations)]
9pub struct FindMapFuture<'a, S, F> {
10    stream: &'a mut S,
11    f: F,
12}
13
14impl<'a, S, F> FindMapFuture<'a, S, F> {
15    pub(super) fn new(stream: &'a mut S, f: F) -> Self {
16        Self { stream, f }
17    }
18}
19
20impl<S: Unpin, F> Unpin for FindMapFuture<'_, S, F> {}
21
22impl<'a, S, B, F> Future for FindMapFuture<'a, S, F>
23where
24    S: Stream + Unpin + Sized,
25    F: FnMut(S::Item) -> Option<B>,
26{
27    type Output = Option<B>;
28
29    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
30        let item = futures_core::ready!(Pin::new(&mut *self.stream).poll_next(cx));
31
32        match item {
33            Some(v) => match (&mut self.f)(v) {
34                Some(v) => Poll::Ready(Some(v)),
35                None => {
36                    cx.waker().wake_by_ref();
37                    Poll::Pending
38                }
39            },
40            None => Poll::Ready(None),
41        }
42    }
43}