pub struct RcConditionalFunction<T, R> { /* private fields */ }Expand description
RcConditionalFunction struct
A single-threaded conditional function that only executes when a
predicate is satisfied. Uses RcFunction and RcPredicate for shared
ownership within a single thread.
This type is typically created by calling RcFunction::when() and is
designed to work with the or_else() method to create if-then-else logic.
§Features
- Shared Ownership: Cloneable via
Rc, multiple owners allowed - Single-Threaded: Not thread-safe, cannot be sent across threads
- Conditional Execution: Only transforms when predicate returns
true - No Lock Overhead: More efficient than
ArcConditionalFunction
§Examples
use prism3_function::{Function, RcFunction};
let double = RcFunction::new(|x: i32| x * 2);
let identity = RcFunction::<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> RcConditionalFunction<T, R>where
T: 'static,
R: 'static,
impl<T, R> RcConditionalFunction<T, R>where
T: 'static,
R: 'static,
Sourcepub fn or_else<F>(&self, else_function: F) -> RcFunction<T, R>where
F: Function<T, R> + 'static,
pub fn or_else<F>(&self, else_function: F) -> RcFunction<T, R>where
F: Function<T, R> + 'static,
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 RcConditionalFunction<T, R>
impl<T, R> Clone for RcConditionalFunction<T, R>
Source§impl<T, R> Debug for RcConditionalFunction<T, R>
impl<T, R> Debug for RcConditionalFunction<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for RcConditionalFunction<T, R>
impl<T, R> !RefUnwindSafe for RcConditionalFunction<T, R>
impl<T, R> !Send for RcConditionalFunction<T, R>
impl<T, R> !Sync for RcConditionalFunction<T, R>
impl<T, R> Unpin for RcConditionalFunction<T, R>
impl<T, R> !UnwindSafe for RcConditionalFunction<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