Trait rayon::iter::par_bridge::ParallelBridge
source · pub trait ParallelBridge: Sized {
// Required method
fn par_bridge(self) -> IterBridge<Self>;
}
Expand description
Conversion trait to convert an Iterator
to a ParallelIterator
.
This creates a “bridge” from a sequential iterator to a parallel one, by distributing its items
across the Rayon thread pool. This has the advantage of being able to parallelize just about
anything, but the resulting ParallelIterator
can be less efficient than if you started with
par_iter
instead. However, it can still be useful for iterators that are difficult to
parallelize by other means, like channels or file or network I/O.
Iterator items are pulled by next()
one at a time, synchronized from each thread that is
ready for work, so this may become a bottleneck if the serial iterator can’t keep up with the
parallel demand. The items are not buffered by IterBridge
, so it’s fine to use this with
large or even unbounded iterators.
The resulting iterator is not guaranteed to keep the order of the original iterator.
§Examples
To use this trait, take an existing Iterator
and call par_bridge
on it. After that, you can
use any of the ParallelIterator
methods:
use rayon::iter::ParallelBridge;
use rayon::prelude::ParallelIterator;
use std::sync::mpsc::channel;
let rx = {
let (tx, rx) = channel();
tx.send("one!");
tx.send("two!");
tx.send("three!");
rx
};
let mut output: Vec<&'static str> = rx.into_iter().par_bridge().collect();
output.sort_unstable();
assert_eq!(&*output, &["one!", "three!", "two!"]);
Required Methods§
sourcefn par_bridge(self) -> IterBridge<Self>
fn par_bridge(self) -> IterBridge<Self>
Creates a bridge from this type to a ParallelIterator
.