diff --git a/src/lib.rs b/src/lib.rs index 997f89d..495e01e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; @@ -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( + self, + m: Map, + f: ForEach, + ) -> Result<(), Box> + 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.