Skip to main content

ParFunctor

Trait ParFunctor 

Source
pub trait ParFunctor: Kind_cdc7cd43dac7585f {
    // Required method
    fn par_map<'a, A: 'a + Send, B: 'a + Send>(
        f: impl Fn(A) -> B + Send + Sync + 'a,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>;
}
Expand description

A type class for data structures that can be mapped over in parallel.

ParFunctor is the parallel counterpart to Functor. Implementors define par_map directly: there is no intermediate Vec conversion imposed by the interface. Types backed by contiguous memory (e.g., VecBrand) can use rayon’s parallel iterators directly; other types may collect to Vec as an implementation detail.

§Laws

ParFunctor instances must satisfy the same laws as Functor:

  • Identity: par_map(identity, fa) = fa.
  • Composition: par_map(|a| f(g(a)), fa) = par_map(f, par_map(g, fa)).

§Thread Safety

All par_* functions require A: Send, B: Send, and closures to be Send + Sync. These bounds apply even when the rayon feature is disabled, so that code compiles identically in both configurations.

Note: The rayon feature must be enabled to use actual parallel execution. Without it, all par_* functions fall back to equivalent sequential operations.

§Examples

ParFunctor laws for Vec:

use fp_library::{
	brands::VecBrand,
	functions::*,
};

let xs = vec![1, 2, 3];

// Identity: par_map(identity, fa) = fa
assert_eq!(par_map::<VecBrand, _, _>(identity, xs.clone()), xs);

// Composition: par_map(|a| f(g(a)), fa) = par_map(f, par_map(g, fa))
let f = |a: i32| a + 1;
let g = |a: i32| a * 2;
assert_eq!(
	par_map::<VecBrand, _, _>(|a| f(g(a)), xs.clone()),
	par_map::<VecBrand, _, _>(f, par_map::<VecBrand, _, _>(g, xs)),
);

Required Methods§

Source

fn par_map<'a, A: 'a + Send, B: 'a + Send>( f: impl Fn(A) -> B + Send + Sync + 'a, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>

Maps a function over a data structure in parallel.

When the rayon feature is enabled, elements are processed across multiple threads. Otherwise falls back to sequential mapping.

§Type Signature

forall A B. (A -> B, Self A) -> Self B

§Type Parameters
  • 'a: The lifetime of the elements.
  • A: The input element type.
  • B: The output element type.
§Parameters
  • f: The function to apply to each element. Must be Send + Sync.
  • fa: The data structure to map over.
§Returns

A new data structure containing the mapped elements.

§Examples
use fp_library::{
	brands::VecBrand,
	classes::par_functor::ParFunctor,
};

let result = VecBrand::par_map(|x: i32| x * 2, vec![1, 2, 3]);
assert_eq!(result, vec![2, 4, 6]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§