Skip to content

Commit 6f9ec66

Browse files
bors[bot]montekki
andauthored
Merge #174
174: adds stream::find_map combinator r=yoshuawuyts a=montekki Adds a `stream::find_map` combinator --- Stdlib: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find_map Ref: #129 Co-authored-by: Fedor Sakharov <[email protected]>
2 parents 6d1e71f + efb8415 commit 6f9ec66

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/stream/stream/find_map.rs

+50
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

+27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
mod all;
2525
mod any;
2626
mod filter_map;
27+
mod find_map;
2728
mod min_by;
2829
mod next;
2930
mod nth;
@@ -34,6 +35,7 @@ pub use take::Take;
3435
use all::AllFuture;
3536
use any::AnyFuture;
3637
use filter_map::FilterMap;
38+
use find_map::FindMapFuture;
3739
use min_by::MinByFuture;
3840
use next::NextFuture;
3941
use nth::NthFuture;
@@ -52,13 +54,15 @@ cfg_if! {
5254
($a:lifetime, $f:tt, $o:ty) => (ImplFuture<$a, $o>);
5355
($a:lifetime, $f:tt, $o:ty, $t1:ty) => (ImplFuture<$a, $o>);
5456
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => (ImplFuture<$a, $o>);
57+
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => (ImplFuture<$a, $o>);
5558

5659
}
5760
} else {
5861
macro_rules! ret {
5962
($a:lifetime, $f:tt, $o:ty) => ($f<$a, Self>);
6063
($a:lifetime, $f:tt, $o:ty, $t1:ty) => ($f<$a, Self, $t1>);
6164
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => ($f<$a, Self, $t1, $t2>);
65+
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => ($f<$a, Self, $t1, $t2, $t3>);
6266
}
6367
}
6468
}
@@ -317,6 +321,29 @@ pub trait Stream {
317321
}
318322
}
319323

324+
/// Applies function to the elements of stream and returns the first non-none result.
325+
///
326+
/// ```
327+
/// # fn main() { async_std::task::block_on(async {
328+
/// #
329+
/// use async_std::prelude::*;
330+
/// use std::collections::VecDeque;
331+
///
332+
/// let mut s: VecDeque<&str> = vec!["lol", "NaN", "2", "5"].into_iter().collect();
333+
/// let first_number = s.find_map(|s| s.parse().ok()).await;
334+
///
335+
/// assert_eq!(first_number, Some(2));
336+
/// #
337+
/// # }) }
338+
/// ```
339+
fn find_map<F, B>(&mut self, f: F) -> ret!('_, FindMapFuture, Option<B>, F, Self::Item, B)
340+
where
341+
Self: Sized,
342+
F: FnMut(Self::Item) -> Option<B>,
343+
{
344+
FindMapFuture::new(self, f)
345+
}
346+
320347
/// Tests if any element of the stream matches a predicate.
321348
///
322349
/// `any()` takes a closure that returns `true` or `false`. It applies

0 commit comments

Comments
 (0)