pub struct ArcConditionalTransformer<T, R> { /* private fields */ }Expand description
ArcConditionalTransformer struct
A thread-safe conditional transformer that only executes when a predicate is
satisfied. Uses ArcTransformer and ArcPredicate for shared ownership
across threads.
This type is typically created by calling ArcTransformer::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 + Sync, safe for concurrent use - Conditional Execution: Only transforms when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic
§Examples
use prism3_function::{Transformer, ArcTransformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let identity = ArcTransformer::<i32, i32>::identity();
let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
let conditional_clone = conditional.clone();
assert_eq!(conditional.apply(5), 10);
assert_eq!(conditional_clone.apply(-5), -5);§Author
Haixing Hu
Implementations§
Source§impl<T, R> ArcConditionalTransformer<T, R>
impl<T, R> ArcConditionalTransformer<T, R>
Sourcepub fn or_else<F>(self, else_transformer: F) -> ArcTransformer<T, R>
pub fn or_else<F>(self, else_transformer: F) -> ArcTransformer<T, R>
Adds an else branch (thread-safe version)
Executes the original transformer when the condition is satisfied, otherwise executes else_transformer.
§Parameters
else_transformer- The transformer for the else branch, can be:- Closure:
|x: T| -> R(must beSend + Sync) ArcTransformer<T, R>,BoxTransformer<T, R>- Any type implementing
Transformer<T, R> + Send + Sync
- Closure:
§Returns
Returns the composed ArcTransformer<T, R>
§Examples
§Using a closure (recommended)
use prism3_function::{Transformer, ArcTransformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
assert_eq!(conditional.apply(5), 10);
assert_eq!(conditional.apply(-5), 5);Trait Implementations§
Source§impl<T, R> Clone for ArcConditionalTransformer<T, R>
impl<T, R> Clone for ArcConditionalTransformer<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for ArcConditionalTransformer<T, R>
impl<T, R> !RefUnwindSafe for ArcConditionalTransformer<T, R>
impl<T, R> Send for ArcConditionalTransformer<T, R>
impl<T, R> Sync for ArcConditionalTransformer<T, R>
impl<T, R> Unpin for ArcConditionalTransformer<T, R>
impl<T, R> !UnwindSafe for ArcConditionalTransformer<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