Skip to content

Commit 45bd0ef

Browse files
committed
adds stream::find_map combinator
1 parent 2c02037 commit 45bd0ef

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/stream/stream/find_map.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::marker::PhantomData;
2+
use std::pin::Pin;
3+
use std::task::{Context, Poll};
4+
5+
#[allow(missing_debug_implementations)]
6+
pub struct FindMapFuture<'a, S, F, T, B> {
7+
stream: &'a mut S,
8+
f: F,
9+
__b: PhantomData<B>,
10+
__t: PhantomData<T>,
11+
}
12+
13+
impl<'a, S, B, F, T> FindMapFuture<'a, S, F, T, B> {
14+
pin_utils::unsafe_pinned!(stream: &'a mut S);
15+
pin_utils::unsafe_unpinned!(f: F);
16+
17+
pub(super) fn new(stream: &'a mut S, f: F) -> Self {
18+
FindMapFuture {
19+
stream,
20+
f,
21+
__b: PhantomData,
22+
__t: PhantomData,
23+
}
24+
}
25+
}
26+
27+
impl<'a, S, B, F> futures_core::future::Future for FindMapFuture<'a, S, F, S::Item, B>
28+
where
29+
S: futures_core::stream::Stream + Unpin + Sized,
30+
F: FnMut(S::Item) -> Option<B>,
31+
{
32+
type Output = Option<B>;
33+
34+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
35+
use futures_core::stream::Stream;
36+
37+
let item = futures_core::ready!(self.as_mut().stream().poll_next(cx));
38+
39+
match item {
40+
Some(v) => match (self.as_mut().f())(v) {
41+
Some(v) => Poll::Ready(Some(v)),
42+
None => {
43+
cx.waker().wake_by_ref();
44+
Poll::Pending
45+
}
46+
},
47+
None => Poll::Ready(None),
48+
}
49+
}
50+
}

src/stream/stream/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
2424
mod all;
2525
mod any;
26+
mod find_map;
2627
mod min_by;
2728
mod next;
2829
mod take;
@@ -31,6 +32,7 @@ pub use take::Take;
3132

3233
use all::AllFuture;
3334
use any::AnyFuture;
35+
use find_map::FindMapFuture;
3436
use min_by::MinByFuture;
3537
use next::NextFuture;
3638

@@ -48,13 +50,15 @@ cfg_if! {
4850
($a:lifetime, $f:tt, $o:ty) => (ImplFuture<$a, $o>);
4951
($a:lifetime, $f:tt, $o:ty, $t1:ty) => (ImplFuture<$a, $o>);
5052
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => (ImplFuture<$a, $o>);
53+
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => (ImplFuture<$a, $o>);
5154

5255
}
5356
} else {
5457
macro_rules! ret {
5558
($a:lifetime, $f:tt, $o:ty) => ($f<$a, Self>);
5659
($a:lifetime, $f:tt, $o:ty, $t1:ty) => ($f<$a, Self, $t1>);
5760
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => ($f<$a, Self, $t1, $t2>);
61+
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => ($f<$a, Self, $t1, $t2, $t3>);
5862
}
5963
}
6064
}
@@ -218,6 +222,29 @@ pub trait Stream {
218222
}
219223
}
220224

225+
/// Applies function to the elements of stream and returns the first non-none result.
226+
///
227+
/// ```
228+
/// # fn main() { async_std::task::block_on(async {
229+
/// #
230+
/// use async_std::prelude::*;
231+
/// use std::collections::VecDeque;
232+
///
233+
/// let mut s: VecDeque<&str> = vec!["lol", "NaN", "2", "5"].into_iter().collect();
234+
/// let first_number = s.find_map(|s| s.parse().ok()).await;
235+
///
236+
/// assert_eq!(first_number, Some(2));
237+
/// #
238+
/// # }) }
239+
/// ```
240+
fn find_map<F, B>(&mut self, f: F) -> ret!('_, FindMapFuture, Option<B>, F, Self::Item, B)
241+
where
242+
Self: Sized,
243+
F: FnMut(Self::Item) -> Option<B>,
244+
{
245+
FindMapFuture::new(self, f)
246+
}
247+
221248
/// Tests if any element of the stream matches a predicate.
222249
///
223250
/// `any()` takes a closure that returns `true` or `false`. It applies

0 commit comments

Comments
 (0)