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

Added parallel_map_for_each #9

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::{
atomic::{AtomicBool, Ordering::SeqCst},
Arc,
};
use std::any::Any;

mod parallel_map;
pub use self::parallel_map::{ParallelMap, ParallelMapBuilder};
Expand Down Expand Up @@ -101,6 +102,27 @@ pub trait IteratorExt {
of(ParallelMapBuilder::new(self)).with_scoped(scope, f)
}

/// `iter.parallel_map_for_each(m, f)` is something like `iter.parallel_map(m).for_each(f)`, but allows to borrow stack variables
/// (i. e. doesn't require `'static`)
fn parallel_map_for_each<Map, ForEach, O>(
self,
m: Map,
f: ForEach,
) -> Result<(), Box<dyn Any + Send + 'static>>
where
Self: Sized,
Self: Iterator,
Map: Send + Clone,
Self::Item: Send,
Map: FnMut(Self::Item) -> O,
O: Send,
ForEach: FnMut(O),
{
crossbeam::thread::scope(move |s| {
self.parallel_map_scoped(s, m).for_each(f);
})
}

/// Run `filter` function in parallel on multiple threads
///
/// A wrapper around [`IteratorExt::parallel_map`] really, so it has similiar properties.
Expand Down