1 use super::noop
::NoopConsumer
;
2 use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator}
;
5 use std
::collections
::LinkedList
;
6 use std
::collections
::{BTreeMap, BTreeSet, HashMap, HashSet}
;
7 use std
::collections
::{BinaryHeap, VecDeque}
;
8 use std
::hash
::{BuildHasher, Hash}
;
10 /// Creates an empty default collection and extends it.
11 fn collect_extended
<C
, I
>(par_iter
: I
) -> C
13 I
: IntoParallelIterator
,
14 C
: ParallelExtend
<I
::Item
> + Default
,
16 let mut collection
= C
::default();
17 collection
.par_extend(par_iter
);
21 /// Collects items from a parallel iterator into a vector.
22 impl<T
> FromParallelIterator
<T
> for Vec
<T
>
26 fn from_par_iter
<I
>(par_iter
: I
) -> Self
28 I
: IntoParallelIterator
<Item
= T
>,
30 collect_extended(par_iter
)
34 /// Collects items from a parallel iterator into a vecdeque.
35 impl<T
> FromParallelIterator
<T
> for VecDeque
<T
>
39 fn from_par_iter
<I
>(par_iter
: I
) -> Self
41 I
: IntoParallelIterator
<Item
= T
>,
43 Vec
::from_par_iter(par_iter
).into()
47 /// Collects items from a parallel iterator into a binaryheap.
48 /// The heap-ordering is calculated serially after all items are collected.
49 impl<T
> FromParallelIterator
<T
> for BinaryHeap
<T
>
53 fn from_par_iter
<I
>(par_iter
: I
) -> Self
55 I
: IntoParallelIterator
<Item
= T
>,
57 Vec
::from_par_iter(par_iter
).into()
61 /// Collects items from a parallel iterator into a freshly allocated
63 impl<T
> FromParallelIterator
<T
> for LinkedList
<T
>
67 fn from_par_iter
<I
>(par_iter
: I
) -> Self
69 I
: IntoParallelIterator
<Item
= T
>,
71 collect_extended(par_iter
)
75 /// Collects (key, value) pairs from a parallel iterator into a
76 /// hashmap. If multiple pairs correspond to the same key, then the
77 /// ones produced earlier in the parallel iterator will be
78 /// overwritten, just as with a sequential iterator.
79 impl<K
, V
, S
> FromParallelIterator
<(K
, V
)> for HashMap
<K
, V
, S
>
83 S
: BuildHasher
+ Default
+ Send
,
85 fn from_par_iter
<I
>(par_iter
: I
) -> Self
87 I
: IntoParallelIterator
<Item
= (K
, V
)>,
89 collect_extended(par_iter
)
93 /// Collects (key, value) pairs from a parallel iterator into a
94 /// btreemap. If multiple pairs correspond to the same key, then the
95 /// ones produced earlier in the parallel iterator will be
96 /// overwritten, just as with a sequential iterator.
97 impl<K
, V
> FromParallelIterator
<(K
, V
)> for BTreeMap
<K
, V
>
102 fn from_par_iter
<I
>(par_iter
: I
) -> Self
104 I
: IntoParallelIterator
<Item
= (K
, V
)>,
106 collect_extended(par_iter
)
110 /// Collects values from a parallel iterator into a hashset.
111 impl<V
, S
> FromParallelIterator
<V
> for HashSet
<V
, S
>
114 S
: BuildHasher
+ Default
+ Send
,
116 fn from_par_iter
<I
>(par_iter
: I
) -> Self
118 I
: IntoParallelIterator
<Item
= V
>,
120 collect_extended(par_iter
)
124 /// Collects values from a parallel iterator into a btreeset.
125 impl<V
> FromParallelIterator
<V
> for BTreeSet
<V
>
129 fn from_par_iter
<I
>(par_iter
: I
) -> Self
131 I
: IntoParallelIterator
<Item
= V
>,
133 collect_extended(par_iter
)
137 /// Collects characters from a parallel iterator into a string.
138 impl FromParallelIterator
<char> for String
{
139 fn from_par_iter
<I
>(par_iter
: I
) -> Self
141 I
: IntoParallelIterator
<Item
= char>,
143 collect_extended(par_iter
)
147 /// Collects characters from a parallel iterator into a string.
148 impl<'a
> FromParallelIterator
<&'a
char> for String
{
149 fn from_par_iter
<I
>(par_iter
: I
) -> Self
151 I
: IntoParallelIterator
<Item
= &'a
char>,
153 collect_extended(par_iter
)
157 /// Collects string slices from a parallel iterator into a string.
158 impl<'a
> FromParallelIterator
<&'a
str> for String
{
159 fn from_par_iter
<I
>(par_iter
: I
) -> Self
161 I
: IntoParallelIterator
<Item
= &'a
str>,
163 collect_extended(par_iter
)
167 /// Collects strings from a parallel iterator into one large string.
168 impl FromParallelIterator
<String
> for String
{
169 fn from_par_iter
<I
>(par_iter
: I
) -> Self
171 I
: IntoParallelIterator
<Item
= String
>,
173 collect_extended(par_iter
)
177 /// Collects string slices from a parallel iterator into a string.
178 impl<'a
> FromParallelIterator
<Cow
<'a
, str>> for String
{
179 fn from_par_iter
<I
>(par_iter
: I
) -> Self
181 I
: IntoParallelIterator
<Item
= Cow
<'a
, str>>,
183 collect_extended(par_iter
)
187 /// Collects an arbitrary `Cow` collection.
189 /// Note, the standard library only has `FromIterator` for `Cow<'a, str>` and
190 /// `Cow<'a, [T]>`, because no one thought to add a blanket implementation
191 /// before it was stabilized.
192 impl<'a
, C
: ?Sized
, T
> FromParallelIterator
<T
> for Cow
<'a
, C
>
195 C
::Owned
: FromParallelIterator
<T
>,
198 fn from_par_iter
<I
>(par_iter
: I
) -> Self
200 I
: IntoParallelIterator
<Item
= T
>,
202 Cow
::Owned(C
::Owned
::from_par_iter(par_iter
))
206 /// Collapses all unit items from a parallel iterator into one.
208 /// This is more useful when combined with higher-level abstractions, like
209 /// collecting to a `Result<(), E>` where you only care about errors:
213 /// use rayon::prelude::*;
215 /// let data = vec![1, 2, 3, 4, 5];
216 /// let res: Result<()> = data.par_iter()
217 /// .map(|x| writeln!(stdout(), "{}", x))
219 /// assert!(res.is_ok());
221 impl FromParallelIterator
<()> for () {
222 fn from_par_iter
<I
>(par_iter
: I
) -> Self
224 I
: IntoParallelIterator
<Item
= ()>,
226 par_iter
.into_par_iter().drive_unindexed(NoopConsumer
)