| 1 | use core::marker::PhantomData; |
| 2 | use core::pin::Pin; |
| 3 | use core::future::Future; |
| 4 | |
| 5 | use crate::stream::Stream; |
| 6 | use crate::task::{Context, Poll}; |
| 7 | |
| 8 | #[doc (hidden)] |
| 9 | #[allow (missing_debug_implementations)] |
| 10 | pub struct AllFuture<'a, S, F, T> { |
| 11 | pub(crate) stream: &'a mut S, |
| 12 | pub(crate) f: F, |
| 13 | pub(crate) _marker: PhantomData<T>, |
| 14 | } |
| 15 | |
| 16 | impl<'a, S, F, T> AllFuture<'a, S, F, T> { |
| 17 | pub(crate) fn new(stream: &'a mut S, f: F) -> Self { |
| 18 | Self { |
| 19 | stream, |
| 20 | f, |
| 21 | _marker: PhantomData, |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<S: Unpin, F, T> Unpin for AllFuture<'_, S, F, T> {} |
| 27 | |
| 28 | impl<S, F> Future for AllFuture<'_, S, F, S::Item> |
| 29 | where |
| 30 | S: Stream + Unpin + Sized, |
| 31 | F: FnMut(S::Item) -> bool, |
| 32 | { |
| 33 | type Output = bool; |
| 34 | |
| 35 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 36 | let next: Option<::Item> = futures_core::ready!(Pin::new(&mut *self.stream).poll_next(cx)); |
| 37 | |
| 38 | match next { |
| 39 | Some(v: ::Item) => { |
| 40 | let result: bool = (&mut self.f)(v); |
| 41 | |
| 42 | if result { |
| 43 | // don't forget to wake this task again to pull the next item from stream |
| 44 | cx.waker().wake_by_ref(); |
| 45 | Poll::Pending |
| 46 | } else { |
| 47 | Poll::Ready(false) |
| 48 | } |
| 49 | } |
| 50 | None => Poll::Ready(true), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |