Skip to content

Commit

Permalink
Merge #497 #498
Browse files Browse the repository at this point in the history
497: impl FromParallelIterator<()> for () r=cuviper a=cuviper

This is more useful when combined with higher-level abstractions, like
collecting to a `Result<(), E>` where you only care about errors.

This is a parallel version of rust-lang/rust#45379.
Cc #496

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
3 parents f37fc75 + 28ca2e4 + e2ad7a2 commit 8b798f2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 10 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,15 @@ 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>>
{
extend(self, par_iter, |string, list| string.reserve(str_len(list)));
}
}


/// Extend a deque with items from a parallel iterator.
impl<T> ParallelExtend<T> for VecDeque<T>
Expand Down
34 changes: 33 additions & 1 deletion src/iter/from_par_iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend};
use super::{FromParallelIterator, IntoParallelIterator, ParallelIterator, ParallelExtend};

use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
Expand Down 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 All @@ -170,3 +179,26 @@ impl<'a, C: ?Sized, T> FromParallelIterator<T> for Cow<'a, C>
Cow::Owned(C::Owned::from_par_iter(par_iter))
}
}

/// Collapses all unit items from a parallel iterator into one.
///
/// This is more useful when combined with higher-level abstractions, like
/// collecting to a `Result<(), E>` where you only care about errors:
///
/// ```
/// use std::io::*;
/// use rayon::prelude::*;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let res: Result<()> = data.par_iter()
/// .map(|x| writeln!(stdout(), "{}", x))
/// .collect();
/// assert!(res.is_ok());
/// ```
impl FromParallelIterator<()> for () {
fn from_par_iter<I>(par_iter: I) -> Self
where I: IntoParallelIterator<Item = ()>
{
par_iter.into_par_iter().for_each(|()| {})
}
}

0 comments on commit 8b798f2

Please sign in to comment.