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::ffi::{OsStr, OsString};
9use std::hash::{BuildHasher, Hash};
10use std::rc::Rc;
11use std::sync::Arc;
12
13fn collect_extended<C, I>(par_iter: I) -> C
15where
16 I: IntoParallelIterator,
17 C: ParallelExtend<I::Item> + Default,
18{
19 let mut collection = C::default();
20 collection.par_extend(par_iter);
21 collection
22}
23
24impl<T> FromParallelIterator<T> for Vec<T>
26where
27 T: Send,
28{
29 fn from_par_iter<I>(par_iter: I) -> Self
30 where
31 I: IntoParallelIterator<Item = T>,
32 {
33 collect_extended(par_iter)
34 }
35}
36
37impl<T> FromParallelIterator<T> for Box<[T]>
39where
40 T: Send,
41{
42 fn from_par_iter<I>(par_iter: I) -> Self
43 where
44 I: IntoParallelIterator<Item = T>,
45 {
46 Vec::from_par_iter(par_iter).into()
47 }
48}
49
50impl<T> FromParallelIterator<T> for Rc<[T]>
52where
53 T: Send,
54{
55 fn from_par_iter<I>(par_iter: I) -> Self
56 where
57 I: IntoParallelIterator<Item = T>,
58 {
59 Vec::from_par_iter(par_iter).into()
60 }
61}
62
63impl<T> FromParallelIterator<T> for Arc<[T]>
65where
66 T: Send,
67{
68 fn from_par_iter<I>(par_iter: I) -> Self
69 where
70 I: IntoParallelIterator<Item = T>,
71 {
72 Vec::from_par_iter(par_iter).into()
73 }
74}
75
76impl<T> FromParallelIterator<T> for VecDeque<T>
78where
79 T: Send,
80{
81 fn from_par_iter<I>(par_iter: I) -> Self
82 where
83 I: IntoParallelIterator<Item = T>,
84 {
85 Vec::from_par_iter(par_iter).into()
86 }
87}
88
89impl<T> FromParallelIterator<T> for BinaryHeap<T>
92where
93 T: Ord + Send,
94{
95 fn from_par_iter<I>(par_iter: I) -> Self
96 where
97 I: IntoParallelIterator<Item = T>,
98 {
99 Vec::from_par_iter(par_iter).into()
100 }
101}
102
103impl<T> FromParallelIterator<T> for LinkedList<T>
106where
107 T: Send,
108{
109 fn from_par_iter<I>(par_iter: I) -> Self
110 where
111 I: IntoParallelIterator<Item = T>,
112 {
113 collect_extended(par_iter)
114 }
115}
116
117impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
122where
123 K: Eq + Hash + Send,
124 V: Send,
125 S: BuildHasher + Default + Send,
126{
127 fn from_par_iter<I>(par_iter: I) -> Self
128 where
129 I: IntoParallelIterator<Item = (K, V)>,
130 {
131 collect_extended(par_iter)
132 }
133}
134
135impl<K, V> FromParallelIterator<(K, V)> for BTreeMap<K, V>
140where
141 K: Ord + Send,
142 V: Send,
143{
144 fn from_par_iter<I>(par_iter: I) -> Self
145 where
146 I: IntoParallelIterator<Item = (K, V)>,
147 {
148 collect_extended(par_iter)
149 }
150}
151
152impl<V, S> FromParallelIterator<V> for HashSet<V, S>
154where
155 V: Eq + Hash + Send,
156 S: BuildHasher + Default + Send,
157{
158 fn from_par_iter<I>(par_iter: I) -> Self
159 where
160 I: IntoParallelIterator<Item = V>,
161 {
162 collect_extended(par_iter)
163 }
164}
165
166impl<V> FromParallelIterator<V> for BTreeSet<V>
168where
169 V: Send + Ord,
170{
171 fn from_par_iter<I>(par_iter: I) -> Self
172 where
173 I: IntoParallelIterator<Item = V>,
174 {
175 collect_extended(par_iter)
176 }
177}
178
179impl FromParallelIterator<char> for String {
181 fn from_par_iter<I>(par_iter: I) -> Self
182 where
183 I: IntoParallelIterator<Item = char>,
184 {
185 collect_extended(par_iter)
186 }
187}
188
189impl<'a> FromParallelIterator<&'a char> for String {
191 fn from_par_iter<I>(par_iter: I) -> Self
192 where
193 I: IntoParallelIterator<Item = &'a char>,
194 {
195 collect_extended(par_iter)
196 }
197}
198
199impl<'a> FromParallelIterator<&'a str> for String {
201 fn from_par_iter<I>(par_iter: I) -> Self
202 where
203 I: IntoParallelIterator<Item = &'a str>,
204 {
205 collect_extended(par_iter)
206 }
207}
208
209impl FromParallelIterator<String> for String {
211 fn from_par_iter<I>(par_iter: I) -> Self
212 where
213 I: IntoParallelIterator<Item = String>,
214 {
215 collect_extended(par_iter)
216 }
217}
218
219impl FromParallelIterator<Box<str>> for String {
221 fn from_par_iter<I>(par_iter: I) -> Self
222 where
223 I: IntoParallelIterator<Item = Box<str>>,
224 {
225 collect_extended(par_iter)
226 }
227}
228
229impl<'a> FromParallelIterator<Cow<'a, str>> for String {
231 fn from_par_iter<I>(par_iter: I) -> Self
232 where
233 I: IntoParallelIterator<Item = Cow<'a, str>>,
234 {
235 collect_extended(par_iter)
236 }
237}
238
239impl<'a> FromParallelIterator<&'a OsStr> for OsString {
241 fn from_par_iter<I>(par_iter: I) -> Self
242 where
243 I: IntoParallelIterator<Item = &'a OsStr>,
244 {
245 collect_extended(par_iter)
246 }
247}
248
249impl FromParallelIterator<OsString> for OsString {
251 fn from_par_iter<I>(par_iter: I) -> Self
252 where
253 I: IntoParallelIterator<Item = OsString>,
254 {
255 collect_extended(par_iter)
256 }
257}
258
259impl<'a> FromParallelIterator<Cow<'a, OsStr>> for OsString {
261 fn from_par_iter<I>(par_iter: I) -> Self
262 where
263 I: IntoParallelIterator<Item = Cow<'a, OsStr>>,
264 {
265 collect_extended(par_iter)
266 }
267}
268
269impl<'a, C: ?Sized, T> FromParallelIterator<T> for Cow<'a, C>
275where
276 C: ToOwned,
277 C::Owned: FromParallelIterator<T>,
278 T: Send,
279{
280 fn from_par_iter<I>(par_iter: I) -> Self
281 where
282 I: IntoParallelIterator<Item = T>,
283 {
284 Cow::Owned(C::Owned::from_par_iter(par_iter))
285 }
286}
287
288impl FromParallelIterator<()> for () {
304 fn from_par_iter<I>(par_iter: I) -> Self
305 where
306 I: IntoParallelIterator<Item = ()>,
307 {
308 par_iter.into_par_iter().drive_unindexed(NoopConsumer)
309 }
310}