Last active
March 29, 2021 16:20
-
-
Save jRimbault/19c49b4a83f7c05a87bd221f84a1a6d0 to your computer and use it in GitHub Desktop.
SequentialFromParallelIterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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