@@ -10,8 +10,39 @@ use current_num_threads;
10
10
11
11
/// Conversion trait to convert an `Iterator` to a `ParallelIterator`.
12
12
///
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
+ /// ```
15
46
pub trait ParallelBridge {
16
47
/// What is the type of the output `ParallelIterator`?
17
48
type Iter : ParallelIterator < Item = Self :: Item > ;
@@ -37,6 +68,11 @@ impl<T: Iterator + Send> ParallelBridge for T
37
68
}
38
69
39
70
/// `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
40
76
#[ derive( Debug ) ]
41
77
pub struct IterParallel < Iter > {
42
78
iter : Iter ,
0 commit comments