Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds stream::find_map combinator #174

Merged
merged 2 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/stream/stream/find_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

#[allow(missing_debug_implementations)]
pub struct FindMapFuture<'a, S, F, T, B> {
stream: &'a mut S,
f: F,
__b: PhantomData<B>,
__t: PhantomData<T>,
}

impl<'a, S, B, F, T> FindMapFuture<'a, S, F, T, B> {
pin_utils::unsafe_pinned!(stream: &'a mut S);
pin_utils::unsafe_unpinned!(f: F);

pub(super) fn new(stream: &'a mut S, f: F) -> Self {
FindMapFuture {
stream,
f,
__b: PhantomData,
__t: PhantomData,
}
}
}

impl<'a, S, B, F> futures_core::future::Future for FindMapFuture<'a, S, F, S::Item, B>
where
S: futures_core::stream::Stream + Unpin + Sized,
F: FnMut(S::Item) -> Option<B>,
{
type Output = Option<B>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures_core::stream::Stream;

let item = futures_core::ready!(self.as_mut().stream().poll_next(cx));

match item {
Some(v) => match (self.as_mut().f())(v) {
Some(v) => Poll::Ready(Some(v)),
None => {
cx.waker().wake_by_ref();
Poll::Pending
}
},
None => Poll::Ready(None),
}
}
}
27 changes: 27 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
mod all;
mod any;
mod filter_map;
mod find_map;
mod min_by;
mod next;
mod nth;
Expand All @@ -34,6 +35,7 @@ pub use take::Take;
use all::AllFuture;
use any::AnyFuture;
use filter_map::FilterMap;
use find_map::FindMapFuture;
use min_by::MinByFuture;
use next::NextFuture;
use nth::NthFuture;
Expand All @@ -52,13 +54,15 @@ cfg_if! {
($a:lifetime, $f:tt, $o:ty) => (ImplFuture<$a, $o>);
($a:lifetime, $f:tt, $o:ty, $t1:ty) => (ImplFuture<$a, $o>);
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => (ImplFuture<$a, $o>);
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => (ImplFuture<$a, $o>);

}
} else {
macro_rules! ret {
($a:lifetime, $f:tt, $o:ty) => ($f<$a, Self>);
($a:lifetime, $f:tt, $o:ty, $t1:ty) => ($f<$a, Self, $t1>);
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty) => ($f<$a, Self, $t1, $t2>);
($a:lifetime, $f:tt, $o:ty, $t1:ty, $t2:ty, $t3:ty) => ($f<$a, Self, $t1, $t2, $t3>);
}
}
}
Expand Down Expand Up @@ -317,6 +321,29 @@ pub trait Stream {
}
}

/// Applies function to the elements of stream and returns the first non-none result.
///
/// ```
/// # fn main() { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*;
/// use std::collections::VecDeque;
///
/// let mut s: VecDeque<&str> = vec!["lol", "NaN", "2", "5"].into_iter().collect();
/// let first_number = s.find_map(|s| s.parse().ok()).await;
///
/// assert_eq!(first_number, Some(2));
/// #
/// # }) }
/// ```
fn find_map<F, B>(&mut self, f: F) -> ret!('_, FindMapFuture, Option<B>, F, Self::Item, B)
where
Self: Sized,
F: FnMut(Self::Item) -> Option<B>,
{
FindMapFuture::new(self, f)
}

/// Tests if any element of the stream matches a predicate.
///
/// `any()` takes a closure that returns `true` or `false`. It applies
Expand Down