Skip to content

Commit 3d54ea2

Browse files
update docs for ParallelBridge and IterParallel
1 parent 3f7a658 commit 3d54ea2

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/iter/par_bridge.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,39 @@ use current_num_threads;
1010

1111
/// Conversion trait to convert an `Iterator` to a `ParallelIterator`.
1212
///
13-
/// This needs to be distinct from `IntoParallelIterator` because that trait is already implemented
14-
/// on a few `Iterator`s, like `std::ops::Range`.
13+
/// This creates a "bridge" from a sequential iterator to a parallel one, by distributing its items
14+
/// across the Rayon thread pool. This has the advantage of being able to parallelize just about
15+
/// anything, but the resulting `ParallelIterator` can be less efficient than if you started with
16+
/// `par_iter` instead. However, it can still be useful for iterators that are difficult to
17+
/// parallelize by other means, like channels or file or network I/O.
18+
///
19+
/// The resulting iterator is not guaranteed to keep the order of the original iterator.
20+
///
21+
/// # Examples
22+
///
23+
/// To use this trait, take an existing `Iterator` and call `par_bridge` on it. After that, you can
24+
/// use any of the `ParallelIterator` methods:
25+
///
26+
/// ```
27+
/// use rayon::iter::ParallelBridge;
28+
/// use rayon::prelude::ParallelIterator;
29+
/// use std::sync::mpsc::channel;
30+
///
31+
/// let rx = {
32+
/// let (tx, rx) = channel();
33+
///
34+
/// tx.send("one!");
35+
/// tx.send("two!");
36+
/// tx.send("three!");
37+
///
38+
/// rx
39+
/// };
40+
///
41+
/// let mut output: Vec<&'static str> = rx.into_iter().par_bridge().collect();
42+
/// output.sort_unstable();
43+
///
44+
/// assert_eq!(&*output, &["one!", "three!", "two!"]);
45+
/// ```
1546
pub trait ParallelBridge {
1647
/// What is the type of the output `ParallelIterator`?
1748
type Iter: ParallelIterator<Item = Self::Item>;
@@ -37,6 +68,11 @@ impl<T: Iterator + Send> ParallelBridge for T
3768
}
3869

3970
/// `IterParallel` is a parallel iterator that wraps a sequential iterator.
71+
///
72+
/// This type is created when using the `par_bridge` method on `ParallelBridge`. See the
73+
/// [`ParallelBridge`] documentation for details.
74+
///
75+
/// [`ParallelBridge`]: trait.ParallelBridge.html
4076
#[derive(Debug)]
4177
pub struct IterParallel<Iter> {
4278
iter: Iter,

0 commit comments

Comments
 (0)