Skip to content

Adds for_each stream combinator #264

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

Merged
merged 1 commit into from
Oct 1, 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
46 changes: 46 additions & 0 deletions src/stream/stream/for_each.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::marker::PhantomData;
use std::pin::Pin;

use crate::future::Future;
use crate::stream::Stream;
use crate::task::{Context, Poll};

#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct ForEachFuture<S, F, T> {
stream: S,
f: F,
__t: PhantomData<T>,
}

impl<S, F, T> ForEachFuture<S, F, T> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(f: F);

pub(super) fn new(stream: S, f: F) -> Self {
ForEachFuture {
stream,
f,
__t: PhantomData,
}
}
}

impl<S, F> Future for ForEachFuture<S, F, S::Item>
where
S: Stream + Sized,
F: FnMut(S::Item),
{
type Output = ();

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));

match next {
Some(v) => (self.as_mut().f())(v),
None => return Poll::Ready(()),
}
}
}
}
37 changes: 37 additions & 0 deletions src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod filter_map;
mod find;
mod find_map;
mod fold;
mod for_each;
mod fuse;
mod inspect;
mod min_by;
Expand All @@ -49,6 +50,7 @@ use filter_map::FilterMap;
use find::FindFuture;
use find_map::FindMapFuture;
use fold::FoldFuture;
use for_each::ForEachFuture;
use min_by::MinByFuture;
use next::NextFuture;
use nth::NthFuture;
Expand Down Expand Up @@ -750,6 +752,41 @@ extension_trait! {
FoldFuture::new(self, init, f)
}

#[doc = r#"
Call a closure on each element of the stream.

# Examples

```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use std::collections::VecDeque;
use std::sync::mpsc::channel;

let (tx, rx) = channel();

let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
let sum = s.for_each(move |x| tx.clone().send(x).unwrap()).await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this method maybe take an async closure as well, similar to #262? Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, like i said i intend to update #262 as soon as this is merged. Or we could do this the other way around: merge #262 and update this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I missed that -- yeah let's go ahead and merge this first!


let v: Vec<_> = rx.iter().collect();

assert_eq!(v, vec![1, 2, 3]);
#
# }) }
```
"#]
fn for_each<F>(
self,
f: F,
) -> impl Future<Output = ()> [ForEachFuture<Self, F, Self::Item>]
where
Self: Sized,
F: FnMut(Self::Item),
{
ForEachFuture::new(self, f)
}

#[doc = r#"
Tests if any element of the stream matches a predicate.

Expand Down