Skip to content

Instantly share code, notes, and snippets.

@jRimbault
Last active March 29, 2021 16:20
Show Gist options
  • Save jRimbault/19c49b4a83f7c05a87bd221f84a1a6d0 to your computer and use it in GitHub Desktop.
Save jRimbault/19c49b4a83f7c05a87bd221f84a1a6d0 to your computer and use it in GitHub Desktop.
SequentialFromParallelIterator
use rayon::iter::ParallelIterator;
use std::iter::FromIterator;
/// Unordered sequential collector
pub trait SequentialFromParallelIterator: ParallelIterator {
fn seq_collect<R>(self, cap: usize) -> R
where
R: FromIterator<Self::Item>,
R: Send,
{
rayon::scope(|scope| {
let (sender, receiver) = crossbeam_channel::bounded(cap);
scope.spawn(move |_| self.for_each(|item| sender.send(item).unwrap()));
receiver.into_iter().collect()
})
}
}
impl<I> SequentialFromParallelIterator for I where I: ParallelIterator {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment