pub struct RcConditionalBiFunction<T, U, R> { /* private fields */ }Expand description
RcConditionalBiFunction struct
A single-threaded conditional bi-function that only executes when a
bi-predicate is satisfied. Uses RcBiFunction and RcBiPredicate for
shared ownership within a single thread.
This type is typically created by calling RcBiFunction::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 computes when bi-predicate returns
true - No Lock Overhead: More efficient than
ArcConditionalBiFunction
§Examples
use prism3_function::{BiFunction, RcBiFunction};
let add = RcBiFunction::new(|x: &i32, y: &i32| *x + *y);
let multiply = RcBiFunction::new(|x: &i32, y: &i32| *x * *y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0).or_else(multiply);
let conditional_clone = conditional.clone();
assert_eq!(conditional.apply(&5, &3), 8);
assert_eq!(conditional_clone.apply(&-5, &3), -15);§Author
Haixing Hu
Implementations§
Source§impl<T, U, R> RcConditionalBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
impl<T, U, R> RcConditionalBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Sourcepub fn or_else<F>(&self, else_function: F) -> RcBiFunction<T, U, R>where
F: BiFunction<T, U, R> + 'static,
pub fn or_else<F>(&self, else_function: F) -> RcBiFunction<T, U, R>where
F: BiFunction<T, U, R> + 'static,
Provides an alternative bi-function for when the predicate is not satisfied
Combines the current conditional bi-function with an alternative bi-function into a new bi-function that implements the following semantics:
When the returned bi-function is called with arguments:
- If the predicate is satisfied, it executes the internal bi-function
- If the predicate is NOT satisfied, it executes the alternative bi-function
§Parameters
else_function- The alternative bi-function to execute when predicate fails
§Returns
Returns a new bi-function that handles both conditional branches
§Examples
ⓘ
let func = RcBiFunction::new(|x: &i32, y: &i32| *x + *y);
let alternative = RcBiFunction::new(|x: &i32, y: &i32| *x * *y);
let conditional = func.when(|x, y| *x > 0 && *y > 0).or_else(alternative);
assert_eq!(conditional.apply(3, 4), 7); // 3 + 4 = 7
assert_eq!(conditional.apply(-2, 4), -8); // -2 * 4 = -8Trait Implementations§
Source§impl<T, U, R> Clone for RcConditionalBiFunction<T, U, R>
impl<T, U, R> Clone for RcConditionalBiFunction<T, U, R>
Source§impl<T, U, R> Debug for RcConditionalBiFunction<T, U, R>
impl<T, U, R> Debug for RcConditionalBiFunction<T, U, R>
Auto Trait Implementations§
impl<T, U, R> Freeze for RcConditionalBiFunction<T, U, R>
impl<T, U, R> !RefUnwindSafe for RcConditionalBiFunction<T, U, R>
impl<T, U, R> !Send for RcConditionalBiFunction<T, U, R>
impl<T, U, R> !Sync for RcConditionalBiFunction<T, U, R>
impl<T, U, R> Unpin for RcConditionalBiFunction<T, U, R>
impl<T, U, R> !UnwindSafe for RcConditionalBiFunction<T, U, 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