Skip to content

Commit 8b798f2

Browse files
committed
Merge #497 #498
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.
3 parents f37fc75 + 28ca2e4 + e2ad7a2 commit 8b798f2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/iter/extend.rs

Lines changed: 10 additions & 0 deletions
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,15 @@ 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+
extend(self, par_iter, |string, list| string.reserve(str_len(list)));
272+
}
273+
}
274+
265275

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

src/iter/from_par_iter.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend};
1+
use super::{FromParallelIterator, IntoParallelIterator, ParallelIterator, ParallelExtend};
22

33
use std::borrow::Cow;
44
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
@@ -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
@@ -170,3 +179,26 @@ impl<'a, C: ?Sized, T> FromParallelIterator<T> for Cow<'a, C>
170179
Cow::Owned(C::Owned::from_par_iter(par_iter))
171180
}
172181
}
182+
183+
/// Collapses all unit items from a parallel iterator into one.
184+
///
185+
/// This is more useful when combined with higher-level abstractions, like
186+
/// collecting to a `Result<(), E>` where you only care about errors:
187+
///
188+
/// ```
189+
/// use std::io::*;
190+
/// use rayon::prelude::*;
191+
///
192+
/// let data = vec![1, 2, 3, 4, 5];
193+
/// let res: Result<()> = data.par_iter()
194+
/// .map(|x| writeln!(stdout(), "{}", x))
195+
/// .collect();
196+
/// assert!(res.is_ok());
197+
/// ```
198+
impl FromParallelIterator<()> for () {
199+
fn from_par_iter<I>(par_iter: I) -> Self
200+
where I: IntoParallelIterator<Item = ()>
201+
{
202+
par_iter.into_par_iter().for_each(|()| {})
203+
}
204+
}

0 commit comments

Comments
 (0)