async_std/stream/stream/
all.rs

1use core::marker::PhantomData;
2use core::pin::Pin;
3use core::future::Future;
4
5use crate::stream::Stream;
6use crate::task::{Context, Poll};
7
8#[doc(hidden)]
9#[allow(missing_debug_implementations)]
10pub 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
16impl<'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
26impl<S: Unpin, F, T> Unpin for AllFuture<'_, S, F, T> {}
27
28impl<S, F> Future for AllFuture<'_, S, F, S::Item>
29where
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 = futures_core::ready!(Pin::new(&mut *self.stream).poll_next(cx));
37
38        match next {
39            Some(v) => {
40                let result = (&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}