Trait rayon::iter::FromParallelIterator
source · pub trait FromParallelIterator<T>where
T: Send,{
// Required method
fn from_par_iter<I>(par_iter: I) -> Self
where I: IntoParallelIterator<Item = T>;
}
Expand description
FromParallelIterator
implements the creation of a collection
from a ParallelIterator
. By implementing
FromParallelIterator
for a given type, you define how it will be
created from an iterator.
FromParallelIterator
is used through ParallelIterator
’s collect()
method.
§Examples
Implementing FromParallelIterator
for your type:
use rayon::prelude::*;
use std::mem;
struct BlackHole {
mass: usize,
}
impl<T: Send> FromParallelIterator<T> for BlackHole {
fn from_par_iter<I>(par_iter: I) -> Self
where I: IntoParallelIterator<Item = T>
{
let par_iter = par_iter.into_par_iter();
BlackHole {
mass: par_iter.count() * mem::size_of::<T>(),
}
}
}
let bh: BlackHole = (0i32..1000).into_par_iter().collect();
assert_eq!(bh.mass, 4000);
Required Methods§
sourcefn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
Creates an instance of the collection from the parallel iterator par_iter
.
If your collection is not naturally parallel, the easiest (and
fastest) way to do this is often to collect par_iter
into a
LinkedList
(via collect_vec_list
) or another intermediate
data structure and then sequentially extend your collection. However,
a more ‘native’ technique is to use the par_iter.fold
or
par_iter.fold_with
methods to create the collection.
Alternatively, if your collection is ‘natively’ parallel, you
can use par_iter.for_each
to process each element in turn.
Object Safety§
Implementations on Foreign Types§
source§impl FromParallelIterator<char> for String
impl FromParallelIterator<char> for String
Collects characters from a parallel iterator into a string.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = char>,
source§impl FromParallelIterator<()> for ()
impl FromParallelIterator<()> for ()
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());
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = ()>,
source§impl FromParallelIterator<Box<str>> for String
impl FromParallelIterator<Box<str>> for String
Collects boxed strings from a parallel iterator into one large string.
fn from_par_iter<I>(par_iter: I) -> Self
source§impl FromParallelIterator<String> for String
impl FromParallelIterator<String> for String
Collects strings from a parallel iterator into one large string.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = String>,
source§impl FromParallelIterator<OsString> for OsString
impl FromParallelIterator<OsString> for OsString
Collects OS-strings from a parallel iterator into one large OS-string.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = OsString>,
source§impl<'a> FromParallelIterator<&'a char> for String
impl<'a> FromParallelIterator<&'a char> for String
Collects characters from a parallel iterator into a string.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = &'a char>,
source§impl<'a> FromParallelIterator<&'a str> for String
impl<'a> FromParallelIterator<&'a str> for String
Collects string slices from a parallel iterator into a string.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = &'a str>,
source§impl<'a> FromParallelIterator<&'a OsStr> for OsString
impl<'a> FromParallelIterator<&'a OsStr> for OsString
Collects OS-string slices from a parallel iterator into an OS-string.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = &'a OsStr>,
source§impl<'a> FromParallelIterator<Cow<'a, str>> for String
impl<'a> FromParallelIterator<Cow<'a, str>> for String
Collects string slices from a parallel iterator into a string.
fn from_par_iter<I>(par_iter: I) -> Self
source§impl<'a> FromParallelIterator<Cow<'a, OsStr>> for OsString
impl<'a> FromParallelIterator<Cow<'a, OsStr>> for OsString
Collects OS-string slices from a parallel iterator into an OS-string.
fn from_par_iter<I>(par_iter: I) -> Self
source§impl<'a, C, T> FromParallelIterator<T> for Cow<'a, C>
impl<'a, C, T> FromParallelIterator<T> for Cow<'a, C>
Collects an arbitrary Cow
collection.
Note, the standard library only has FromIterator
for Cow<'a, str>
and
Cow<'a, [T]>
, because no one thought to add a blanket implementation
before it was stabilized.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<A, B, FromA, FromB> FromParallelIterator<(A, B)> for (FromA, FromB)where
A: Send,
B: Send,
FromA: Send + FromParallelIterator<A>,
FromB: Send + FromParallelIterator<B>,
impl<A, B, FromA, FromB> FromParallelIterator<(A, B)> for (FromA, FromB)where
A: Send,
B: Send,
FromA: Send + FromParallelIterator<A>,
FromB: Send + FromParallelIterator<B>,
fn from_par_iter<I>(pi: I) -> Selfwhere
I: IntoParallelIterator<Item = (A, B)>,
source§impl<C, T> FromParallelIterator<Option<T>> for Option<C>where
C: FromParallelIterator<T>,
T: Send,
impl<C, T> FromParallelIterator<Option<T>> for Option<C>where
C: FromParallelIterator<T>,
T: Send,
Collect an arbitrary Option
-wrapped collection.
If any item is None
, then all previous items collected are discarded,
and it returns only None
.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = Option<T>>,
source§impl<C, T, E> FromParallelIterator<Result<T, E>> for Result<C, E>
impl<C, T, E> FromParallelIterator<Result<T, E>> for Result<C, E>
Collect an arbitrary Result
-wrapped collection.
If any item is Err
, then all previous Ok
items collected are
discarded, and it returns that error. If there are multiple errors, the
one returned is not deterministic.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = Result<T, E>>,
source§impl<K, V> FromParallelIterator<(K, V)> for BTreeMap<K, V>
impl<K, V> FromParallelIterator<(K, V)> for BTreeMap<K, V>
Collects (key, value) pairs from a parallel iterator into a btreemap. If multiple pairs correspond to the same key, then the ones produced earlier in the parallel iterator will be overwritten, just as with a sequential iterator.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = (K, V)>,
source§impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
Collects (key, value) pairs from a parallel iterator into a hashmap. If multiple pairs correspond to the same key, then the ones produced earlier in the parallel iterator will be overwritten, just as with a sequential iterator.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = (K, V)>,
source§impl<L, R, A, B> FromParallelIterator<Either<L, R>> for (A, B)
impl<L, R, A, B> FromParallelIterator<Either<L, R>> for (A, B)
fn from_par_iter<I>(pi: I) -> Selfwhere
I: IntoParallelIterator<Item = Either<L, R>>,
source§impl<T> FromParallelIterator<T> for Box<[T]>where
T: Send,
impl<T> FromParallelIterator<T> for Box<[T]>where
T: Send,
Collects items from a parallel iterator into a boxed slice.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<T> FromParallelIterator<T> for BinaryHeap<T>
impl<T> FromParallelIterator<T> for BinaryHeap<T>
Collects items from a parallel iterator into a binaryheap. The heap-ordering is calculated serially after all items are collected.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<T> FromParallelIterator<T> for LinkedList<T>where
T: Send,
impl<T> FromParallelIterator<T> for LinkedList<T>where
T: Send,
Collects items from a parallel iterator into a freshly allocated linked list.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<T> FromParallelIterator<T> for VecDeque<T>where
T: Send,
impl<T> FromParallelIterator<T> for VecDeque<T>where
T: Send,
Collects items from a parallel iterator into a vecdeque.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<T> FromParallelIterator<T> for Rc<[T]>where
T: Send,
impl<T> FromParallelIterator<T> for Rc<[T]>where
T: Send,
Collects items from a parallel iterator into a reference-counted slice.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<T> FromParallelIterator<T> for Arc<[T]>where
T: Send,
impl<T> FromParallelIterator<T> for Arc<[T]>where
T: Send,
Collects items from a parallel iterator into an atomically-reference-counted slice.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<T> FromParallelIterator<T> for Vec<T>where
T: Send,
impl<T> FromParallelIterator<T> for Vec<T>where
T: Send,
Collects items from a parallel iterator into a vector.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = T>,
source§impl<V> FromParallelIterator<V> for BTreeSet<V>
impl<V> FromParallelIterator<V> for BTreeSet<V>
Collects values from a parallel iterator into a btreeset.
fn from_par_iter<I>(par_iter: I) -> Selfwhere
I: IntoParallelIterator<Item = V>,
source§impl<V, S> FromParallelIterator<V> for HashSet<V, S>
impl<V, S> FromParallelIterator<V> for HashSet<V, S>
Collects values from a parallel iterator into a hashset.