Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 26 additions & 55 deletions src/iter/from_par_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,65 +176,36 @@ where
}
}

/// Collects characters from a parallel iterator into a string.
impl FromParallelIterator<char> for String {
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = char>,
{
collect_extended(par_iter)
}
}
macro_rules! collect_string {
($desc:literal, $item:ty $(, $a:lifetime)?) => {
#[doc = concat!("Collects ", $desc, " from a parallel iterator into a string.")]
impl$(<$a>)? FromParallelIterator<$item> for String {
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = $item>,
{
collect_extended(par_iter)
}
}

/// Collects characters from a parallel iterator into a string.
impl<'a> FromParallelIterator<&'a char> for String {
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = &'a char>,
{
collect_extended(par_iter)
#[doc = concat!("Collects ", $desc, " from a parallel iterator into a boxed string.")]
impl$(<$a>)? FromParallelIterator<$item> for Box<str> {
fn from_par_iter<I>(par_iter: I) -> Self
where
I: IntoParallelIterator<Item = $item>,
{
String::from_par_iter(par_iter).into_boxed_str()
}
}
}
}

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

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

/// 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
where
I: IntoParallelIterator<Item = Cow<'a, str>>,
{
collect_extended(par_iter)
}
}
collect_string!("characters", char);
collect_string!("characters", &'a char, 'a);
collect_string!("string slices", &'a str, 'a);
collect_string!("string slices", Cow<'a, str>, 'a);
collect_string!("boxed strings", Box<str>);
collect_string!("strings", String);

/// Collects OS-string slices from a parallel iterator into an OS-string.
impl<'a> FromParallelIterator<&'a OsStr> for OsString {
Expand Down
18 changes: 18 additions & 0 deletions src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,15 @@ impl<'data, T: Sync> IntoParallelIterator for &'data [T] {
}
}

impl<'data, T: Sync> IntoParallelIterator for &'data Box<[T]> {
type Item = &'data T;
type Iter = Iter<'data, T>;

fn into_par_iter(self) -> Self::Iter {
Iter { slice: self }
}
}

impl<'data, T: Send> IntoParallelIterator for &'data mut [T] {
type Item = &'data mut T;
type Iter = IterMut<'data, T>;
Expand All @@ -779,6 +788,15 @@ impl<'data, T: Send> IntoParallelIterator for &'data mut [T] {
}
}

impl<'data, T: Send> IntoParallelIterator for &'data mut Box<[T]> {
type Item = &'data mut T;
type Iter = IterMut<'data, T>;

fn into_par_iter(self) -> Self::Iter {
IterMut { slice: self }
}
}

/// Parallel iterator over immutable items in a slice
#[derive(Debug)]
pub struct Iter<'data, T> {
Expand Down
9 changes: 9 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ impl<T: Send> IntoParallelIterator for Vec<T> {
}
}

impl<T: Send> IntoParallelIterator for Box<[T]> {
type Item = T;
type Iter = IntoIter<T>;

fn into_par_iter(self) -> Self::Iter {
IntoIter { vec: self.into() }
}
}

impl<T: Send> ParallelIterator for IntoIter<T> {
type Item = T;

Expand Down