pub struct ArcBiFunction<T, U, R> { /* private fields */ }Expand description
ArcBiFunction - thread-safe bi-function wrapper
A thread-safe, clonable bi-function wrapper suitable for multi-threaded scenarios. Can be called multiple times and shared across threads.
§Features
- Based on:
Arc<dyn Fn(&T, &U) -> R + Send + Sync> - Ownership: Shared ownership via reference counting
- Reusability: Can be called multiple times (borrows inputs each time)
- Thread Safety: Thread-safe (
Send + Syncrequired) - Clonable: Cheap cloning via
Arc::clone
§Author
Haixing Hu
Implementations§
Source§impl<T, U, R> ArcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
impl<T, U, R> ArcBiFunction<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) -> ArcConditionalBiFunction<T, U, R>
pub fn when<P>(&self, predicate: P) -> ArcConditionalBiFunction<T, U, R>
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::{ArcBiFunction, BiFunction};
use std::sync::Arc;
let add = ArcBiFunction::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) -> ArcBiFunction<T, U, S>
pub fn and_then<S, F>(&self, after: F) -> ArcBiFunction<T, U, S>
Chains execution with another two-parameter function, executing the current function first, then the subsequent function.
§Parameters
after- The subsequent two-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::{ArcBiFunction, BiFunction};
use std::sync::Arc;
let add = ArcBiFunction::new(|x: i32, y: i32| x + y);
let multiply_by_two = ArcBiFunction::new(|x: i32, y: i32| x * y * 2);
let chained = add.and_then(multiply_by_two);
assert_eq!(chained.apply(2, 3), 10); // (2+3) * 2 = 10Source§impl<T, U, R> ArcBiFunction<T, U, R>
impl<T, U, R> ArcBiFunction<T, U, R>
Sourcepub fn constant(value: R) -> ArcBiFunction<T, U, R>
pub fn constant(value: R) -> ArcBiFunction<T, U, R>
Creates a constant function
§Examples
/// rust /// use prism3_function::{ArcBiFunction, BiFunction}; /// /// let constant = ArcBiFunction::constant("hello"); /// assert_eq!(constant.apply(123, "test"), "hello"); ///
Trait Implementations§
Source§impl<T, U, R> BiFunction<T, U, R> for ArcBiFunction<T, U, R>
impl<T, U, R> BiFunction<T, U, R> for ArcBiFunction<T, U, R>
Source§fn apply(&self, first: &T, second: &U) -> R
fn apply(&self, first: &T, second: &U) -> R
Source§fn into_box(self) -> BoxBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn into_box(self) -> BoxBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn into_rc(self) -> RcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn into_rc(self) -> RcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn into_arc(self) -> ArcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn into_arc(self) -> ArcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn into_once(self) -> BoxBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn into_once(self) -> BoxBiFunctionOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn to_box(&self) -> BoxBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn to_box(&self) -> BoxBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn to_rc(&self) -> RcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn to_rc(&self) -> RcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn to_arc(&self) -> ArcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn to_arc(&self) -> ArcBiFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn to_fn(&self) -> impl Fn(&T, &U) -> R
fn to_fn(&self) -> impl Fn(&T, &U) -> R
&self. Read more