Skip to main content

async_std/stream/stream/
find.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use crate::stream::Stream;
5use crate::task::{Context, Poll};
6
7#[doc(hidden)]
8#[allow(missing_debug_implementations)]
9pub struct FindFuture<'a, S, P> {
10    stream: &'a mut S,
11    p: P,
12}
13
14impl<'a, S, P> FindFuture<'a, S, P> {
15    pub(super) fn new(stream: &'a mut S, p: P) -> Self {
16        Self { stream, p }
17    }
18}
19
20impl<S: Unpin, P> Unpin for FindFuture<'_, S, P> {}
21
22impl<'a, S, P> Future for FindFuture<'a, S, P>
23where
24    S: Stream + Unpin + Sized,
25    P: FnMut(&S::Item) -> bool,
26{
27    type Output = Option<S::Item>;
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) if (&mut self.p)(&v) => Poll::Ready(Some(v)),
34            Some(_) => {
35                cx.waker().wake_by_ref();
36                Poll::Pending
37            }
38            None => Poll::Ready(None),
39        }
40    }
41}