Skip to content

Commit 78bafbb

Browse files
committed
Sketch outch rfind
1 parent d0ef48c commit 78bafbb

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/stream/double_ended/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod nth_back;
2+
mod rfind;
23

34
use nth_back::NthBackFuture;
5+
use rfind::RFindFuture;
46

57
extension_trait! {
68
use crate::stream::Stream;
@@ -53,5 +55,17 @@ extension_trait! {
5355
{
5456
NthBackFuture::new(self, n)
5557
}
58+
59+
fn rfind<P>(
60+
&mut self,
61+
p: P,
62+
) -> impl Future<Output = Option<Self::Item>> + '_ [RFindFuture<'_, Self, P>]
63+
where
64+
Self: Unpin + Sized,
65+
P: FnMut(&Self::Item) -> bool,
66+
{
67+
RFindFuture::new(self, p)
68+
}
69+
5670
}
5771
}

src/stream/double_ended/rfind.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::task::{Context, Poll};
2+
use std::future::Future;
3+
use std::pin::Pin;
4+
5+
use crate::stream::DoubleEndedStream;
6+
7+
pub struct RFindFuture<'a, S, P> {
8+
stream: &'a mut S,
9+
p: P,
10+
}
11+
12+
impl<'a, S, P> RFindFuture<'a, S, P> {
13+
pub(super) fn new(stream: &'a mut S, p: P) -> Self {
14+
RFindFuture { stream, p }
15+
}
16+
}
17+
18+
impl<S: Unpin, P> Unpin for RFindFuture<'_, S, P> {}
19+
20+
impl<'a, S, P> Future for RFindFuture<'a, S, P>
21+
where
22+
S: DoubleEndedStream + Unpin + Sized,
23+
P: FnMut(&S::Item) -> bool,
24+
{
25+
type Output = Option<S::Item>;
26+
27+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
28+
let item = futures_core::ready!(Pin::new(&mut *self.stream).poll_next_back(cx));
29+
30+
match item {
31+
Some(v) if (&mut self.p)(&v) => Poll::Ready(Some(v)),
32+
Some(_) => {
33+
cx.waker().wake_by_ref();
34+
Poll::Pending
35+
}
36+
None => Poll::Ready(None),
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)