Struct easy_parallel::Parallel

source ·
pub struct Parallel<'a, T> { /* private fields */ }
Expand description

A builder that runs closures in parallel.

Implementations§

source§

impl<'a, T> Parallel<'a, T>

source

pub fn new() -> Parallel<'a, T>

Creates a builder for running closures in parallel.

Examples
use easy_parallel::Parallel;

let p = Parallel::<()>::new();
Examples found in repository?
examples/par_sum.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
fn par_sum(v: &[i32]) -> i32 {
    const THRESHOLD: usize = 100;

    if v.len() <= THRESHOLD {
        v.iter().copied().sum()
    } else {
        let half = (v.len() + 1) / 2;
        let sums = Parallel::new().each(v.chunks(half), par_sum).run();
        sums.into_iter().sum()
    }
}
source

pub fn add<F>(self, f: F) -> Parallel<'a, T>where F: FnOnce() -> T + Send + 'a, T: Send + 'a,

Adds a closure to the list.

Examples
use easy_parallel::Parallel;

Parallel::new()
    .add(|| println!("hello from a thread"))
    .run();
source

pub fn each<A, I, F>(self, iter: I, f: F) -> Parallel<'a, T>where I: IntoIterator<Item = A>, F: FnOnce(A) -> T + Clone + Send + 'a, A: Send + 'a, T: Send + 'a,

Adds a cloned closure for each item in an iterator.

Each clone of the closure takes an item as an argument.

Examples
use easy_parallel::Parallel;

Parallel::new()
    .each(0..5, |i| println!("hello from thread #{}", i))
    .run();
Examples found in repository?
examples/par_sum.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
fn par_sum(v: &[i32]) -> i32 {
    const THRESHOLD: usize = 100;

    if v.len() <= THRESHOLD {
        v.iter().copied().sum()
    } else {
        let half = (v.len() + 1) / 2;
        let sums = Parallel::new().each(v.chunks(half), par_sum).run();
        sums.into_iter().sum()
    }
}
source

pub fn collect<C>(self) -> Cwhere T: Send + 'a, C: FromIterator<T> + Extend<T>,

Runs each closure on a separate thread and collects their results.

Results are collected in the order in which closures were added. One of the closures always runs on the main thread because there is no point in spawning an extra thread for it.

If a closure panics, panicking will resume in the main thread after all threads are joined.

Examples
use easy_parallel::Parallel;
use std::thread;
use std::time::Duration;

let res = Parallel::new()
    .each(1..=3, |i| 10 * i)
    .add(|| 100)
    .collect::<Vec<_>>();

assert_eq!(res, [10, 20, 30, 100]);
source

pub fn run(self) -> Vec<T>where T: Send + 'a,

Runs each closure on a separate thread and collects their results.

Results are collected in the order in which closures were added. One of the closures always runs on the main thread because there is no point in spawning an extra thread for it.

If a closure panics, panicking will resume in the main thread after all threads are joined.

Examples
use easy_parallel::Parallel;
use std::thread;
use std::time::Duration;

let res = Parallel::new()
    .each(1..=3, |i| 10 * i)
    .add(|| 100)
    .run();

assert_eq!(res, [10, 20, 30, 100]);
Examples found in repository?
examples/par_sum.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
fn par_sum(v: &[i32]) -> i32 {
    const THRESHOLD: usize = 100;

    if v.len() <= THRESHOLD {
        v.iter().copied().sum()
    } else {
        let half = (v.len() + 1) / 2;
        let sums = Parallel::new().each(v.chunks(half), par_sum).run();
        sums.into_iter().sum()
    }
}
source

pub fn finish<F, R>(self, f: F) -> (Vec<T>, R)where F: FnOnce() -> R, T: Send + 'a,

Finishes with a closure to run on the main thread, starts threads, and collects results.

Results are collected in the order in which closures were added.

If a closure panics, panicking will resume in the main thread after all threads are joined.

Examples
use easy_parallel::Parallel;
use std::thread;
use std::time::Duration;

let (res, ()) = Parallel::new()
    .each(1..=3, |i| 10 * i)
    .finish(|| println!("Waiting for results"));

assert_eq!(res, [10, 20, 30]);
source

pub fn finish_in<F, R, C>(self, f: F) -> (C, R)where F: FnOnce() -> R, T: Send + 'a, C: FromIterator<T>,

Finishes with a closure to run on the main thread, starts threads, and collects results into an arbitrary container.

Results are collected in the order in which closures were added.

If a closure panics, panicking will resume in the main thread after all threads are joined.

Examples
use easy_parallel::Parallel;
use std::thread;
use std::time::Duration;

let (res, ()) = Parallel::new()
    .each(1..=3, |i| 10 * i)
    .finish_in::<_, _, Vec<i32>>(|| println!("Waiting for results"));

assert_eq!(res, [10, 20, 30]);

Trait Implementations§

source§

impl<T> Debug for Parallel<'_, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for Parallel<'_, T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for Parallel<'a, T>

§

impl<'a, T> Send for Parallel<'a, T>

§

impl<'a, T> !Sync for Parallel<'a, T>

§

impl<'a, T> Unpin for Parallel<'a, T>

§

impl<'a, T> !UnwindSafe for Parallel<'a, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.