Skip to main content

async_std/stream/double_ended_stream/
rfind.rs

1use core::task::{Context, Poll};
2use core::future::Future;
3use core::pin::Pin;
4
5use crate::stream::DoubleEndedStream;
6
7#[doc(hidden)]
8#[allow(missing_debug_implementations)]
9pub struct RFindFuture<'a, S, P> {
10    stream: &'a mut S,
11    p: P,
12}
13
14impl<'a, S, P> RFindFuture<'a, S, P> {
15    pub(super) fn new(stream: &'a mut S, p: P) -> Self {
16        RFindFuture { stream, p }
17    }
18}
19
20impl<S: Unpin, P> Unpin for RFindFuture<'_, S, P> {}
21
22impl<'a, S, P> Future for RFindFuture<'a, S, P>
23where
24    S: DoubleEndedStream + 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_back(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}