pub trait ParFilterable: ParFunctor + ParCompactable {
// Provided methods
fn par_filter_map<'a, A: 'a + Send, B: 'a + Send>(
f: impl Fn(A) -> Option<B> + Send + Sync + 'a,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B> { ... }
fn par_filter<'a, A: 'a + Send>(
f: impl Fn(&A) -> bool + Send + Sync + 'a,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A> { ... }
}Expand description
A type class for data structures that can be filtered and filter-mapped in parallel.
ParFilterable is the parallel counterpart to Filterable,
extending ParFunctor and ParCompactable.
All methods have default implementations based on par_map and
par_compact. However, it is recommended to override
ParFilterable::par_filter_map and ParFilterable::par_filter with single-pass
implementations to avoid the intermediate allocation created by the default.
- If
ParFilterable::par_filter_mapis overridden,ParFilterable::par_filteris automatically derived from it (noClonebound required).
§Laws
ParFilterable instances must satisfy the same laws as Filterable:
- Identity:
par_filter_map(Some, fa) = fa. - Composition:
par_filter_map(|a| r(a).and_then(l), fa) = par_filter_map(l, par_filter_map(r, fa)). - Consistency:
par_filter(p, fa) = par_filter_map(|a| if p(&a) { Some(a) } else { None }, 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
use fp_library::{
brands::VecBrand,
functions::*,
};
let v = vec![1, 2, 3, 4, 5];
let mapped: Vec<i32> = par_filter_map::<VecBrand, _, _>(
|x: i32| if x % 2 == 0 { Some(x * 10) } else { None },
v.clone(),
);
assert_eq!(mapped, vec![20, 40]);
let filtered: Vec<i32> = par_filter::<VecBrand, _>(|x: &i32| x % 2 == 0, v);
assert_eq!(filtered, vec![2, 4]);Provided Methods§
Sourcefn par_filter_map<'a, A: 'a + Send, B: 'a + Send>(
f: impl Fn(A) -> Option<B> + Send + Sync + 'a,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
fn par_filter_map<'a, A: 'a + Send, B: 'a + Send>( f: impl Fn(A) -> Option<B> + Send + Sync + 'a, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
Maps and filters a data structure in parallel, discarding elements for which f returns
None.
The default implementation uses par_map followed by
par_compact. Override this method with a single-pass
implementation for better performance.
§Type Signature
forall A B. (A -> Option 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, returning anOption. Must beSend + Sync.fa: The data structure to filter and map.
§Returns
A new data structure containing only the values where f returned Some.
§Examples
use fp_library::{
brands::VecBrand,
classes::par_filterable::ParFilterable,
};
let result = VecBrand::par_filter_map(
|x: i32| if x % 2 == 0 { Some(x * 10) } else { None },
vec![1, 2, 3, 4, 5],
);
assert_eq!(result, vec![20, 40]);Sourcefn par_filter<'a, A: 'a + Send>(
f: impl Fn(&A) -> bool + Send + Sync + 'a,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
fn par_filter<'a, A: 'a + Send>( f: impl Fn(&A) -> bool + Send + Sync + 'a, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
Filters a data structure in parallel, retaining only elements satisfying f.
The default implementation derives from par_filter_map.
No Clone bound is required: ownership of each element is passed to the closure,
which either returns Some(a) (retain) or None (discard).
Override this method with a single-pass implementation for better performance.
§Type Signature
forall A. (&A -> bool, Self A) -> Self A
§Type Parameters
'a: The lifetime of the elements.A: The element type.
§Parameters
f: The predicate. Must beSend + Sync.fa: The data structure to filter.
§Returns
A new data structure containing only the elements that satisfy f.
§Examples
use fp_library::{
brands::VecBrand,
classes::par_filterable::ParFilterable,
};
let result = VecBrand::par_filter(|x: &i32| x % 2 == 0, vec![1, 2, 3, 4, 5]);
assert_eq!(result, vec![2, 4]);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.