pub struct RcBiMutatingFunction<T, U, R> { /* private fields */ }Expand description
RcBiMutatingFunction - single-threaded bi-mutating-function wrapper
A single-threaded, clonable bi-mutating-function wrapper optimized for scenarios that require sharing without thread-safety overhead.
§Features
- Based on:
Rc<dyn Fn(&mut T, &mut U) -> R> - Ownership: Shared ownership via reference counting (non-atomic)
- Reusability: Can be called multiple times (borrows inputs mutably each time)
- Thread Safety: Not thread-safe (no
Send + Sync) - Clonable: Cheap cloning via
Rc::clone
§Author
Haixing Hu
Implementations§
Source§impl<T, U, R> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
impl<T, U, R> RcBiMutatingFunction<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) -> RcConditionalBiMutatingFunction<T, U, R>where
P: BiPredicate<T, U> + 'static,
pub fn when<P>(&self, predicate: P) -> RcConditionalBiMutatingFunction<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::{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) -> RcBiMutatingFunction<T, U, S>where
S: 'static,
F: MutatingFunction<R, S> + 'static,
pub fn and_then<S, F>(&self, after: F) -> RcBiMutatingFunction<T, U, S>where
S: 'static,
F: MutatingFunction<R, S> + 'static,
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> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: Clone + 'static,
impl<T, U, R> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> RcBiMutatingFunction<T, U, R>
pub fn constant(value: R) -> RcBiMutatingFunction<T, U, R>
Creates a constant function
§Examples
/// rust /// use prism3_function::{RcBiMutatingFunction, BiFunction}; /// /// let constant = RcBiMutatingFunction::constant("hello"); /// assert_eq!(constant.apply(123, "test"), "hello"); ///
Trait Implementations§
Source§impl<T, U, R> BiMutatingFunction<T, U, R> for RcBiMutatingFunction<T, U, R>
impl<T, U, R> BiMutatingFunction<T, U, R> for RcBiMutatingFunction<T, U, R>
Source§fn apply(&self, first: &mut T, second: &mut U) -> R
fn apply(&self, first: &mut T, second: &mut U) -> R
Source§fn into_box(self) -> BoxBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn into_box(self) -> BoxBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn into_rc(self) -> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn into_rc(self) -> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn into_fn(self) -> impl Fn(&mut T, &mut U) -> R
fn into_fn(self) -> impl Fn(&mut T, &mut U) -> R
Source§fn to_box(&self) -> BoxBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn to_box(&self) -> BoxBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn to_rc(&self) -> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
fn to_rc(&self) -> RcBiMutatingFunction<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Source§fn to_fn(&self) -> impl Fn(&mut T, &mut U) -> R
fn to_fn(&self) -> impl Fn(&mut T, &mut U) -> R
&self. Read more