From 12485de2231183a62a15e891230bfe03227a6aa2 Mon Sep 17 00:00:00 2001 From: Askar Safin Date: Mon, 31 Jul 2023 18:13:25 +0300 Subject: [PATCH] Added parallel_map_for_each --- src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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.