pub struct ArcConditionalMapper<T, R> { /* private fields */ }Expand description
ArcConditionalMapper struct
A thread-safe conditional mapper that only executes when a predicate
is satisfied. Uses ArcMapper and ArcPredicate for shared
ownership across threads.
This type is typically created by calling ArcMapper::when() and is
designed to work with the or_else() method to create if-then-else
logic.
§Features
- Shared Ownership: Cloneable via
Arc, multiple owners allowed - Thread-Safe: Implements
Send, safe for concurrent use - Conditional Execution: Only maps when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic
§Examples
use prism3_function::{Mapper, ArcMapper};
let mut mapper = ArcMapper::new(|x: i32| x * 2)
.when(|x: &i32| *x > 0)
.or_else(|x: i32| -x);
let mut mapper_clone = mapper.clone();
assert_eq!(mapper.apply(5), 10);
assert_eq!(mapper_clone.apply(-5), 5);§Author
Haixing Hu
Implementations§
Source§impl<T, R> ArcConditionalMapper<T, R>
impl<T, R> ArcConditionalMapper<T, R>
Sourcepub fn or_else<F>(self, else_mapper: F) -> ArcMapper<T, R>
pub fn or_else<F>(self, else_mapper: F) -> ArcMapper<T, R>
Adds an else branch (thread-safe version)
Executes the original mapper when the condition is satisfied, otherwise executes else_mapper.
§Parameters
else_mapper- The mapper for the else branch, can be:- Closure:
|x: T| -> R(must beSend) ArcMapper<T, R>,BoxMapper<T, R>- Any type implementing
Mapper<T, R> + Send
- Closure:
§Returns
Returns the composed ArcMapper<T, R>
§Examples
use prism3_function::{Mapper, ArcMapper};
let mut mapper = ArcMapper::new(|x: i32| x * 2)
.when(|x: &i32| *x > 0)
.or_else(|x: i32| -x);
assert_eq!(mapper.apply(5), 10);
assert_eq!(mapper.apply(-5), 5);Trait Implementations§
Source§impl<T, R> Clone for ArcConditionalMapper<T, R>
impl<T, R> Clone for ArcConditionalMapper<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for ArcConditionalMapper<T, R>
impl<T, R> !RefUnwindSafe for ArcConditionalMapper<T, R>
impl<T, R> Send for ArcConditionalMapper<T, R>
impl<T, R> Sync for ArcConditionalMapper<T, R>
impl<T, R> Unpin for ArcConditionalMapper<T, R>
impl<T, R> !UnwindSafe for ArcConditionalMapper<T, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more