pub struct BoxBiFunctionOnce<T, U, R> { /* private fields */ }Expand description
BoxBiFunctionOnce - consuming bi-function wrapper based on
Box<dyn FnOnce>
A bi-function wrapper that provides single ownership with one-time use semantics. Consumes self and borrows both input values.
§Features
- Based on:
Box<dyn FnOnce(&T, &U) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can only be called once (consumes self)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement)
§Author
Haixing Hu
Implementations§
Source§impl<T, U, R> BoxBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
impl<T, U, R> BoxBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new bi-function.
Wraps the provided closure in the appropriate smart pointer type for this bi-function implementation.
Sourcepub fn new_with_name<F>(name: &str, f: F) -> Self
pub fn new_with_name<F>(name: &str, f: F) -> Self
Creates a new named bi-function.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
Creates a new named bi-function with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalBiFunctionOnce<T, U, R>where
P: BiPredicate<T, U> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalBiFunctionOnce<T, U, R>where
P: BiPredicate<T, U> + 'static,
Creates a conditional two-parameter function that executes based on bi-predicate result.
§Parameters
predicate- The bi-predicate to determine whether to execute the function operation
§Returns
Returns a conditional two-parameter function that only executes
when the predicate returns true.
§Examples
use prism3_function::{BoxBiFunction, BiFunction};
let add = BoxBiFunction::new(|x: i32, y: i32| x + y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
assert_eq!(conditional.or_else(|_, _| 0).apply(2, 3), 5); // executed
assert_eq!(conditional.or_else(|_, _| 0).apply(-1, 3), 0); // not executedSourcepub fn and_then<S, F>(self, after: F) -> BoxBiFunctionOnce<T, U, S>where
S: 'static,
F: FunctionOnce<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxBiFunctionOnce<T, U, S>where
S: 'static,
F: FunctionOnce<R, S> + 'static,
Chains execution with another two-parameter function, executing the current function first, then the subsequent function.
§Parameters
after- The subsequent one-parameter function to execute after the current function completes
§Returns
Returns a new two-parameter function that executes the current function and the subsequent function in sequence.
§Examples
use prism3_function::{BoxBiFunction, BoxFunction};
let add = BoxBiFunction::new(|x: i32, y: i32| x + y);
let multiply_by_two = BoxFunction::new(|z: i32| z * 2);
let chained = add.and_then(multiply_by_two);
assert_eq!(chained.apply(2, 3), 10); // (2+3) * 2 = 10Source§impl<T, U, R> BoxBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: Clone + 'static,
impl<T, U, R> BoxBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> BoxBiFunctionOnce<T, U, R>
pub fn constant(value: R) -> BoxBiFunctionOnce<T, U, R>
Creates a constant function
§Examples
/// rust /// use prism3_function::{BoxBiFunctionOnce, BiFunction}; /// /// let constant = BoxBiFunctionOnce::constant("hello"); /// assert_eq!(constant.apply(123, "test"), "hello"); ///