| 1 | use core::future::Future; |
| 2 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll}; |
| 4 | |
| 5 | use crate::stream::Stream; |
| 6 | |
| 7 | #[doc (hidden)] |
| 8 | #[allow (missing_debug_implementations)] |
| 9 | pub struct FindMapFuture<'a, S, F> { |
| 10 | stream: &'a mut S, |
| 11 | f: F, |
| 12 | } |
| 13 | |
| 14 | impl<'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 | |
| 20 | impl<S: Unpin, F> Unpin for FindMapFuture<'_, S, F> {} |
| 21 | |
| 22 | impl<'a, S, B, F> Future for FindMapFuture<'a, S, F> |
| 23 | where |
| 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: Option<::Item> = futures_core::ready!(Pin::new(&mut *self.stream).poll_next(cx)); |
| 31 | |
| 32 | match item { |
| 33 | Some(v: ::Item) => match (&mut self.f)(v) { |
| 34 | Some(v: B) => 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 | } |
| 44 | |