Skip to content

Commit

Permalink
Merge #498
Browse files Browse the repository at this point in the history
498: FromParallelIterator and ParallelExtend Cow for String r=cuviper a=cuviper

Parallel version of rust-lang/rust#41449.
  • Loading branch information
bors[bot] committed Jan 14, 2018
2 parents 1f0bb74 + ed8374d commit 1771795
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/iter/extend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{ParallelExtend, IntoParallelIterator, ParallelIterator};

use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::{BuildHasher, Hash};
use std::collections::LinkedList;
Expand Down Expand Up @@ -262,6 +263,36 @@ impl ParallelExtend<String> for String {
}
}

/// Extend a string with string slices from a parallel iterator.
impl<'a> ParallelExtend<Cow<'a, str>> for String {
fn par_extend<I>(&mut self, par_iter: I)
where I: IntoParallelIterator<Item = Cow<'a, str>>
{
// This is like `extend`, but `Extend<Cow<'a, str>> for String`
// wasn't added until Rust 1.19, so we can't use it directly yet.
let list = par_iter
.into_par_iter()
.fold(Vec::new, |mut vec, elem| {
vec.push(elem);
vec
})
.map(|vec| {
let mut list = LinkedList::new();
list.push_back(vec);
list
})
.reduce(LinkedList::new, |mut list1, mut list2| {
list1.append(&mut list2);
list1
});

self.reserve(str_len(&list));
for vec in list {
self.extend(vec.iter().map(|cow| &**cow));
}
}
}


/// Extend a deque with items from a parallel iterator.
impl<T> ParallelExtend<T> for VecDeque<T>
Expand Down
9 changes: 9 additions & 0 deletions src/iter/from_par_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ impl FromParallelIterator<String> for String {
}
}

/// Collect string slices from a parallel iterator into a string.
impl<'a> FromParallelIterator<Cow<'a, str>> for String {
fn from_par_iter<I>(par_iter: I) -> Self
where I: IntoParallelIterator<Item = Cow<'a, str>>
{
collect_extended(par_iter)
}
}

/// Collect an arbitrary `Cow` collection.
///
/// Note, the standard library only has `FromIterator` for `Cow<'a, str>` and
Expand Down

0 comments on commit 1771795

Please sign in to comment.