pub struct BoxConditionalBiFunctionOnce<T, U, R> { /* private fields */ }Expand description
BoxConditionalBiFunctionOnce struct
A conditional consuming bi-function that only executes when a bi-predicate
is satisfied. Uses BoxBiFunctionOnce and BoxBiPredicate for single
ownership semantics.
This type is typically created by calling BoxBiFunctionOnce::when() and
is designed to work with the or_else() method to create if-then-else logic.
§Features
- Single Ownership: Not cloneable, consumes
selfon use - One-time Use: Can only be called once
- Conditional Execution: Only computes when bi-predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic
§Examples
§With or_else Branch
use prism3_function::{BiFunctionOnce, BoxBiFunctionOnce};
let add = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x + *y);
let multiply = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x * *y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply);
assert_eq!(conditional.apply(&5, &3), 8); // when branch executed
let add2 = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x + *y);
let multiply2 = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x * *y);
let conditional2 = add2.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply2);
assert_eq!(conditional2.apply(&-5, &3), -15); // or_else branch executed§Author
Haixing Hu
Implementations§
Source§impl<T, U, R> BoxConditionalBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
impl<T, U, R> BoxConditionalBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Sourcepub fn or_else<F>(self, else_function: F) -> BoxBiFunctionOnce<T, U, R>where
F: BiFunctionOnce<T, U, R> + 'static,
pub fn or_else<F>(self, else_function: F) -> BoxBiFunctionOnce<T, U, R>where
F: BiFunctionOnce<T, U, R> + 'static,
Provides an alternative function for when the predicate is not satisfied
Combines the current conditional bifunction with an alternative bifunction into a new bifunction that implements the following semantics:
When the returned bifunction is called with two arguments:
- If the predicate is satisfied, it executes the internal bifunction
- If the predicate is NOT satisfied, it executes the alternative bifunction
§Parameters
else_function- The alternative bifunction to execute when predicate fails
§Returns
Returns a new bifunction that handles both conditional branches
§Examples
ⓘ
let func = BoxBiFunction::new(|x: i32, y: i32| x + y);
let alternative = BoxBiFunction::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 (predicate satisfied)
assert_eq!(conditional.apply(-2, 4), -8); // -2 * 4 = -8 (predicate failed)Trait Implementations§
Source§impl<T, U, R> Debug for BoxConditionalBiFunctionOnce<T, U, R>
impl<T, U, R> Debug for BoxConditionalBiFunctionOnce<T, U, R>
Auto Trait Implementations§
impl<T, U, R> Freeze for BoxConditionalBiFunctionOnce<T, U, R>
impl<T, U, R> !RefUnwindSafe for BoxConditionalBiFunctionOnce<T, U, R>
impl<T, U, R> !Send for BoxConditionalBiFunctionOnce<T, U, R>
impl<T, U, R> !Sync for BoxConditionalBiFunctionOnce<T, U, R>
impl<T, U, R> Unpin for BoxConditionalBiFunctionOnce<T, U, R>
impl<T, U, R> !UnwindSafe for BoxConditionalBiFunctionOnce<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