pub struct RcStatefulMutatingFunction<T, R> { /* private fields */ }Expand description
RcStatefulMutatingFunction struct
A stateful mutating function implementation based on
Rc<RefCell<dyn FnMut(&mut T) -> R>> for single-threaded shared
ownership scenarios. This type allows multiple references to the same
function without the overhead of thread safety.
§Features
- Shared Ownership: Cloneable via
Rc, multiple owners allowed - Single-Threaded: Not thread-safe, cannot be sent across threads
- Stateful: Can modify captured environment (uses
FnMut) - Chainable: Method chaining via
&self(non-consuming) - Performance: More efficient than
ArcStatefulMutatingFunction(no locking)
§Use Cases
Choose RcStatefulMutatingFunction when:
- The function needs to be shared within a single thread for stateful operations
- Thread safety is not required
- Performance is important (avoiding lock overhead)
§Examples
use prism3_function::{StatefulMutatingFunction,
RcStatefulMutatingFunction};
let counter = {
let mut count = 0;
RcStatefulMutatingFunction::new(move |x: &mut i32| {
count += 1;
*x *= 2;
count
})
};
let mut clone = counter.clone();
let mut value = 5;
assert_eq!(clone.apply(&mut value), 1);§Author
Haixing Hu
Implementations§
Source§impl<T, R> RcStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
impl<T, R> RcStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new function.
Wraps the provided closure in the appropriate smart pointer type for this 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 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 function with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn when<P>(
&self,
predicate: P,
) -> RcConditionalStatefulMutatingFunction<T, R>where
P: Predicate<T> + 'static,
pub fn when<P>(
&self,
predicate: P,
) -> RcConditionalStatefulMutatingFunction<T, R>where
P: Predicate<T> + 'static,
Creates a conditional function that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the function operation
§Returns
Returns a conditional function that only executes when the
predicate returns true.
§Examples
use prism3_function::{ArcFunction, Function};
use std::sync::Arc;
let double = ArcFunction::new(|x: i32| x * 2);
let conditional = double.when(|value: &i32| *value > 0);
assert_eq!(conditional.or_else(|_| 0).apply(5), 10); // executed
assert_eq!(conditional.or_else(|_| 0).apply(-3), 0); // not executedSourcepub fn and_then<S, F>(&self, after: F) -> RcStatefulMutatingFunction<T, S>where
S: 'static,
F: Function<R, S> + 'static,
pub fn and_then<S, F>(&self, after: F) -> RcStatefulMutatingFunction<T, S>where
S: 'static,
F: Function<R, S> + 'static,
Chains execution with another function, executing the current function first, then the subsequent function.
§Parameters
after- The subsequent function to execute after the current function completes
§Returns
Returns a new function that executes the current function and the subsequent function in sequence.
§Examples
use prism3_function::{ArcFunction, Function};
use std::sync::Arc;
let double = ArcFunction::new(|x: i32| x * 2);
let to_string = ArcFunction::new(|x: i32| x.to_string());
let chained = double.and_then(to_string);
assert_eq!(chained.apply(5), "10".to_string());Source§impl<T> RcStatefulMutatingFunction<T, T>where
T: Clone + 'static,
impl<T> RcStatefulMutatingFunction<T, T>where
T: Clone + 'static,
Sourcepub fn identity() -> RcStatefulMutatingFunction<T, T>
pub fn identity() -> RcStatefulMutatingFunction<T, T>
Creates an identity function
§Examples
/// rust /// use prism3_function::RcStatefulMutatingFunction; /// /// let mut identity = RcStatefulMutatingFunction::<i32, i32>::identity(); /// let mut value = 42; /// assert_eq!(identity.apply(&mut value), 42); ///
Trait Implementations§
Source§impl<T, R> Clone for RcStatefulMutatingFunction<T, R>
impl<T, R> Clone for RcStatefulMutatingFunction<T, R>
Source§impl<T, R> Debug for RcStatefulMutatingFunction<T, R>
impl<T, R> Debug for RcStatefulMutatingFunction<T, R>
Source§impl<T, R> Display for RcStatefulMutatingFunction<T, R>
impl<T, R> Display for RcStatefulMutatingFunction<T, R>
Source§impl<T, R> StatefulMutatingFunction<T, R> for RcStatefulMutatingFunction<T, R>
impl<T, R> StatefulMutatingFunction<T, R> for RcStatefulMutatingFunction<T, R>
Source§fn apply(&mut self, t: &mut T) -> R
fn apply(&mut self, t: &mut T) -> R
Source§fn into_box(self) -> BoxStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
fn into_box(self) -> BoxStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
BoxStatefulMutatingFunction<T, R>. Read moreSource§fn into_rc(self) -> RcStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
fn into_rc(self) -> RcStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
RcStatefulMutatingFunction<T, R>. Read moreSource§fn into_fn(self) -> impl FnMut(&mut T) -> R
fn into_fn(self) -> impl FnMut(&mut T) -> R
FnMut(&mut T) -> R closure. Read moreSource§fn to_box(&self) -> BoxStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
fn to_box(&self) -> BoxStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
Source§fn to_rc(&self) -> RcStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
fn to_rc(&self) -> RcStatefulMutatingFunction<T, R>where
T: 'static,
R: 'static,
Source§fn into_once(self) -> BoxMutatingFunctionOnce<T, R>where
T: 'static,
R: 'static,
fn into_once(self) -> BoxMutatingFunctionOnce<T, R>where
T: 'static,
R: 'static,
Source§fn to_once(&self) -> BoxMutatingFunctionOnce<T, R>where
T: 'static,
R: 'static,
fn to_once(&self) -> BoxMutatingFunctionOnce<T, R>where
T: 'static,
R: 'static,
Source§fn into_arc(self) -> ArcStatefulMutatingFunction<T, R>
fn into_arc(self) -> ArcStatefulMutatingFunction<T, R>
ArcStatefulMutatingFunction<T, R>. Read more