rayon/iter/
from_par_iter.rs1use super::noop::NoopConsumer;
2use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
3
4use std::borrow::Cow;
5use std::collections::LinkedList;
6use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
7use std::collections::{BinaryHeap, VecDeque};
8use std::hash::{BuildHasher, Hash};
9
10fn collect_extended<C, I>(par_iter: I) -> C
12where
13 I: IntoParallelIterator,
14 C: ParallelExtend<I::Item> + Default,
15{
16 let mut collection = C::default();
17 collection.par_extend(par_iter);
18 collection
19}
20
21impl<T> FromParallelIterator<T> for Vec<T>
23where
24 T: Send,
25{
26 fn from_par_iter<I>(par_iter: I) -> Self
27 where
28 I: IntoParallelIterator<Item = T>,
29 {
30 collect_extended(par_iter)
31 }
32}
33
34impl<T> FromParallelIterator<T> for VecDeque<T>
36where
37 T: Send,
38{
39 fn from_par_iter<I>(par_iter: I) -> Self
40 where
41 I: IntoParallelIterator<Item = T>,
42 {
43 Vec::from_par_iter(par_iter).into()
44 }
45}
46
47impl<T> FromParallelIterator<T> for BinaryHeap<T>
50where
51 T: Ord + Send,
52{
53 fn from_par_iter<I>(par_iter: I) -> Self
54 where
55 I: IntoParallelIterator<Item = T>,
56 {
57 Vec::from_par_iter(par_iter).into()
58 }
59}
60
61impl<T> FromParallelIterator<T> for LinkedList<T>
64where
65 T: Send,
66{
67 fn from_par_iter<I>(par_iter: I) -> Self
68 where
69 I: IntoParallelIterator<Item = T>,
70 {
71 collect_extended(par_iter)
72 }
73}
74
75impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
80where
81 K: Eq + Hash + Send,
82 V: Send,
83 S: BuildHasher + Default + Send,
84{
85 fn from_par_iter<I>(par_iter: I) -> Self
86 where
87 I: IntoParallelIterator<Item = (K, V)>,
88 {
89 collect_extended(par_iter)
90 }
91}
92
93impl<K, V> FromParallelIterator<(K, V)> for BTreeMap<K, V>
98where
99 K: Ord + Send,
100 V: Send,
101{
102 fn from_par_iter<I>(par_iter: I) -> Self
103 where
104 I: IntoParallelIterator<Item = (K, V)>,
105 {
106 collect_extended(par_iter)
107 }
108}
109
110impl<V, S> FromParallelIterator<V> for HashSet<V, S>
112where
113 V: Eq + Hash + Send,
114 S: BuildHasher + Default + Send,
115{
116 fn from_par_iter<I>(par_iter: I) -> Self
117 where
118 I: IntoParallelIterator<Item = V>,
119 {
120 collect_extended(par_iter)
121 }
122}
123
124impl<V> FromParallelIterator<V> for BTreeSet<V>
126where
127 V: Send + Ord,
128{
129 fn from_par_iter<I>(par_iter: I) -> Self
130 where
131 I: IntoParallelIterator<Item = V>,
132 {
133 collect_extended(par_iter)
134 }
135}
136
137impl FromParallelIterator<char> for String {
139 fn from_par_iter<I>(par_iter: I) -> Self
140 where
141 I: IntoParallelIterator<Item = char>,
142 {
143 collect_extended(par_iter)
144 }
145}
146
147impl<'a> FromParallelIterator<&'a char> for String {
149 fn from_par_iter<I>(par_iter: I) -> Self
150 where
151 I: IntoParallelIterator<Item = &'a char>,
152 {
153 collect_extended(par_iter)
154 }
155}
156
157impl<'a> FromParallelIterator<&'a str> for String {
159 fn from_par_iter<I>(par_iter: I) -> Self
160 where
161 I: IntoParallelIterator<Item = &'a str>,
162 {
163 collect_extended(par_iter)
164 }
165}
166
167impl FromParallelIterator<String> for String {
169 fn from_par_iter<I>(par_iter: I) -> Self
170 where
171 I: IntoParallelIterator<Item = String>,
172 {
173 collect_extended(par_iter)
174 }
175}
176
177impl<'a> FromParallelIterator<Cow<'a, str>> for String {
179 fn from_par_iter<I>(par_iter: I) -> Self
180 where
181 I: IntoParallelIterator<Item = Cow<'a, str>>,
182 {
183 collect_extended(par_iter)
184 }
185}
186
187impl<'a, C: ?Sized, T> FromParallelIterator<T> for Cow<'a, C>
193where
194 C: ToOwned,
195 C::Owned: FromParallelIterator<T>,
196 T: Send,
197{
198 fn from_par_iter<I>(par_iter: I) -> Self
199 where
200 I: IntoParallelIterator<Item = T>,
201 {
202 Cow::Owned(C::Owned::from_par_iter(par_iter))
203 }
204}
205
206impl FromParallelIterator<()> for () {
222 fn from_par_iter<I>(par_iter: I) -> Self
223 where
224 I: IntoParallelIterator<Item = ()>,
225 {
226 par_iter.into_par_iter().drive_unindexed(NoopConsumer)
227 }
228}