pub struct ArcConditionalStatefulFunction<T, R> { /* private fields */ }Expand description
ArcConditionalStatefulFunction struct
A thread-safe conditional function that only executes when a predicate
is satisfied. Uses ArcStatefulFunction and ArcPredicate for shared
ownership across threads.
This type is typically created by calling ArcStatefulFunction::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::{StatefulFunction, ArcStatefulFunction};
let mut function = ArcStatefulFunction::new(|x: i32| x * 2)
.when(|x: &i32| *x > 0)
.or_else(|x: i32| -x);
let mut function_clone = function.clone();
assert_eq!(function.apply(5), 10);
assert_eq!(function_clone.apply(-5), 5);§Author
Haixing Hu
Implementations§
Source§impl<T, R> ArcConditionalStatefulFunction<T, R>where
T: 'static,
R: 'static,
impl<T, R> ArcConditionalStatefulFunction<T, R>where
T: 'static,
R: 'static,
Sourcepub fn or_else<F>(&self, else_function: F) -> ArcStatefulFunction<T, R>
pub fn or_else<F>(&self, else_function: F) -> ArcStatefulFunction<T, R>
Provides an alternative function for when the predicate is not satisfied
Combines the current conditional function with an alternative function into a new function that implements the following semantics:
When the returned function is called with an argument:
- If the predicate is satisfied, it executes the internal function
- If the predicate is NOT satisfied, it executes the alternative function
§Parameters
else_function- The alternative function to execute when predicate fails
§Returns
Returns a new function that handles both conditional branches
§Examples
ⓘ
let func = ArcFunction::new(|x: i32| x * 2);
let alternative = ArcFunction::new(|x: i32| x + 10);
let conditional = func.when(|x| *x > 0).or_else(alternative);
assert_eq!(conditional.apply(5), 10); // 5 * 2 = 10
assert_eq!(conditional.apply(-3), 7); // -3 + 10 = 7Trait Implementations§
Source§impl<T, R> Clone for ArcConditionalStatefulFunction<T, R>
impl<T, R> Clone for ArcConditionalStatefulFunction<T, R>
Source§impl<T, R> Debug for ArcConditionalStatefulFunction<T, R>
impl<T, R> Debug for ArcConditionalStatefulFunction<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for ArcConditionalStatefulFunction<T, R>
impl<T, R> !RefUnwindSafe for ArcConditionalStatefulFunction<T, R>
impl<T, R> Send for ArcConditionalStatefulFunction<T, R>
impl<T, R> Sync for ArcConditionalStatefulFunction<T, R>
impl<T, R> Unpin for ArcConditionalStatefulFunction<T, R>
impl<T, R> !UnwindSafe for ArcConditionalStatefulFunction<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