Skip to content

Commit 1771795

Browse files
committed
Merge #498
498: FromParallelIterator and ParallelExtend Cow for String r=cuviper a=cuviper Parallel version of rust-lang/rust#41449.
2 parents 1f0bb74 + ed8374d commit 1771795

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/iter/extend.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{ParallelExtend, IntoParallelIterator, ParallelIterator};
22

3+
use std::borrow::Cow;
34
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
45
use std::hash::{BuildHasher, Hash};
56
use std::collections::LinkedList;
@@ -262,6 +263,36 @@ impl ParallelExtend<String> for String {
262263
}
263264
}
264265

266+
/// Extend a string with string slices from a parallel iterator.
267+
impl<'a> ParallelExtend<Cow<'a, str>> for String {
268+
fn par_extend<I>(&mut self, par_iter: I)
269+
where I: IntoParallelIterator<Item = Cow<'a, str>>
270+
{
271+
// This is like `extend`, but `Extend<Cow<'a, str>> for String`
272+
// wasn't added until Rust 1.19, so we can't use it directly yet.
273+
let list = par_iter
274+
.into_par_iter()
275+
.fold(Vec::new, |mut vec, elem| {
276+
vec.push(elem);
277+
vec
278+
})
279+
.map(|vec| {
280+
let mut list = LinkedList::new();
281+
list.push_back(vec);
282+
list
283+
})
284+
.reduce(LinkedList::new, |mut list1, mut list2| {
285+
list1.append(&mut list2);
286+
list1
287+
});
288+
289+
self.reserve(str_len(&list));
290+
for vec in list {
291+
self.extend(vec.iter().map(|cow| &**cow));
292+
}
293+
}
294+
}
295+
265296

266297
/// Extend a deque with items from a parallel iterator.
267298
impl<T> ParallelExtend<T> for VecDeque<T>

src/iter/from_par_iter.rs

+9
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ impl FromParallelIterator<String> for String {
154154
}
155155
}
156156

157+
/// Collect string slices from a parallel iterator into a string.
158+
impl<'a> FromParallelIterator<Cow<'a, str>> for String {
159+
fn from_par_iter<I>(par_iter: I) -> Self
160+
where I: IntoParallelIterator<Item = Cow<'a, str>>
161+
{
162+
collect_extended(par_iter)
163+
}
164+
}
165+
157166
/// Collect an arbitrary `Cow` collection.
158167
///
159168
/// Note, the standard library only has `FromIterator` for `Cow<'a, str>` and

0 commit comments

Comments
 (0)