Trait AsPar

Source
pub trait AsPar<'a, T: Send + Sync> {
    type ConIter: ConcurrentIter;

    // Required method
    fn par(&'a self) -> ParEmpty<Self::ConIter>;
}
Expand description

Non-consuming conversion into a parallel iterator.

Every type that implements IntoConcurrentIter implements AsPar.

See crate::IntoPar for consuming conversion of common collections into parallel iterator.

Converting into a parallel iterator is achieved using the par() method.

§Examples

use orx_parallel::*;

let vec = vec![10usize; 42];
let seq = vec.iter().sum::<usize>();
let par = vec.par().copied().sum();
assert_eq!(par, seq);

let seq = (10..420).filter(|x| x % 2 == 1).map(|x| 2 * x).sum();
let par = (10..420).par().filter(|x| x % 2 == 1).map(|x| 2 * x).sum();
assert_eq!(par, seq);

let names = ["john", "doe", "foo", "bar"].map(String::from);
let seq = names.iter().map(|x| x.len()).reduce(|a, b| a + b);
let par = names.as_slice().into_par().map(|x| x.len()).reduce(|a, b| a + b);
assert_eq!(par, seq);

Required Associated Types§

Source

type ConIter: ConcurrentIter

Underlying concurrent iterator which provides the input elements to the defined parallel computation.

Required Methods§

Source

fn par(&'a self) -> ParEmpty<Self::ConIter>

Non-consuming conversion into a parallel iterator.

Every type that implements IntoConcurrentIter implements AsPar.

See crate::IntoPar for consuming conversion of common collections into parallel iterator.

Converting into a parallel iterator is achieved using the par() method.

§Examples
use orx_parallel::*;

let vec = vec![10usize; 42];
let seq = vec.iter().sum::<usize>();
let par = vec.par().copied().sum();
assert_eq!(par, seq);

let seq = (10..420).filter(|x| x % 2 == 1).map(|x| 2 * x).sum();
let par = (10..420).par().filter(|x| x % 2 == 1).map(|x| 2 * x).sum();
assert_eq!(par, seq);

let names = ["john", "doe", "foo", "bar"].map(String::from);
let seq = names.iter().map(|x| x.len()).reduce(|a, b| a + b);
let par = names.as_slice().into_par().map(|x| x.len()).reduce(|a, b| a + b);
assert_eq!(par, seq);

Implementations on Foreign Types§

Source§

impl<'a, K: Send + Sync + 'a, V: Send + Sync + 'a> AsPar<'a, (K, V)> for BTreeMap<K, V>

Source§

type ConIter = ConIterOfIter<(&'a K, &'a V), Iter<'a, K, V>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, K: Send + Sync + 'a, V: Send + Sync + 'a> AsPar<'a, (K, V)> for HashMap<K, V>

Source§

type ConIter = ConIterOfIter<(&'a K, &'a V), Iter<'a, K, V>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for &'a [T]

Source§

type ConIter = ConIterOfSlice<'a, T>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for BinaryHeap<T>

Source§

type ConIter = ConIterOfIter<&'a T, Iter<'a, T>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for BTreeSet<T>

Source§

type ConIter = ConIterOfIter<&'a T, Iter<'a, T>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for LinkedList<T>

Source§

type ConIter = ConIterOfIter<&'a T, Iter<'a, T>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for VecDeque<T>

Source§

type ConIter = ConIterOfIter<&'a T, Iter<'a, T>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for Vec<T>

Source§

type ConIter = ConIterOfSlice<'a, T>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, T: Send + Sync + 'a> AsPar<'a, T> for HashSet<T>

Source§

type ConIter = ConIterOfIter<&'a T, Iter<'a, T>>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Source§

impl<'a, const N: usize, T: Send + Sync + 'a> AsPar<'a, T> for [T; N]

Source§

type ConIter = ConIterOfSlice<'a, T>

Source§

fn par(&'a self) -> ParEmpty<Self::ConIter>

Implementors§