Skip to content

Commit

Permalink
Merge #1061
Browse files Browse the repository at this point in the history
1061: Add more `FromParallelIterator` and `ParallelExtend` r=cuviper a=cuviper

These all have sequential equivalents in the standard library.

* `impl<T> FromParallelIterator<T> for Box<[T]>`
* `impl<T> FromParallelIterator<T> for Rc<[T]>`
* `impl<T> FromParallelIterator<T> for Arc<[T]>`
* `impl FromParallelIterator<Box<str>> for String`
* `impl ParallelExtend<Box<str>> for String`


Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed Jun 23, 2023
2 parents 4f8e061 + 09be018 commit 6a9deff
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/iter/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,16 @@ impl ParallelExtend<String> for String {
}
}

/// Extends a string with boxed strings from a parallel iterator.
impl ParallelExtend<Box<str>> for String {
fn par_extend<I>(&mut self, par_iter: I)
where
I: IntoParallelIterator<Item = Box<str>>,
{
extend!(self, par_iter, string_extend);
}
}

/// Extends 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)
Expand Down
51 changes: 51 additions & 0 deletions src/iter/from_par_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::collections::LinkedList;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::{BinaryHeap, VecDeque};
use std::hash::{BuildHasher, Hash};
use std::rc::Rc;
use std::sync::Arc;

/// Creates an empty default collection and extends it.
fn collect_extended<C, I>(par_iter: I) -> C
Expand All @@ -31,6 +33,45 @@ where
}
}

/// Collects items from a parallel iterator into a boxed slice.
impl<T> FromParallelIterator<T> for Box<[T]>
where
T: Send,
{
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = T>,
{
Vec::from_par_iter(par_iter).into()
}
}

/// Collects items from a parallel iterator into a reference-counted slice.
impl<T> FromParallelIterator<T> for Rc<[T]>
where
T: Send,
{
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = T>,
{
Vec::from_par_iter(par_iter).into()
}
}

/// Collects items from a parallel iterator into an atomically-reference-counted slice.
impl<T> FromParallelIterator<T> for Arc<[T]>
where
T: Send,
{
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = T>,
{
Vec::from_par_iter(par_iter).into()
}
}

/// Collects items from a parallel iterator into a vecdeque.
impl<T> FromParallelIterator<T> for VecDeque<T>
where
Expand Down Expand Up @@ -174,6 +215,16 @@ impl FromParallelIterator<String> for String {
}
}

/// Collects boxed strings from a parallel iterator into one large string.
impl FromParallelIterator<Box<str>> for String {
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = Box<str>>,
{
collect_extended(par_iter)
}
}

/// Collects 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
Expand Down

0 comments on commit 6a9deff

Please sign in to comment.