pub struct RcMutator<T> { /* private fields */ }Expand description
RcMutator struct
A stateless mutator implementation based on Rc<dyn Fn(&mut T)> for
single-threaded shared ownership scenarios. This type allows multiple
references to the same mutator 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
- Stateless: Cannot modify captured environment (uses
FnnotFnMut) - Chainable: Method chaining via
&self(non-consuming) - Performance: More efficient than
ArcMutator(no locking)
§Use Cases
Choose RcMutator when:
- The mutator needs to be shared within a single thread for stateless operations
- Thread safety is not required
- Performance is important (avoiding lock overhead)
§Examples
use prism3_function::{Mutator, RcMutator};
let mutator = RcMutator::new(|x: &mut i32| *x *= 2);
let clone = mutator.clone();
let mut value = 5;
mutator.apply(&mut value);
assert_eq!(value, 10);§Author
Haixing Hu
Implementations§
Source§impl<T> RcMutator<T>where
T: 'static,
impl<T> RcMutator<T>where
T: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new mutator.
Wraps the provided closure in the appropriate smart pointer type for this mutator 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 mutator.
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 mutator with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn noop() -> Self
pub fn noop() -> Self
Creates a no-operation mutator.
Creates a mutator that does nothing when called. Useful for default values or placeholder implementations.
§Returns
Returns a new mutator instance that performs no operation.
Sourcepub fn when<P>(&self, predicate: P) -> RcConditionalMutator<T>where
P: Predicate<T> + 'static,
pub fn when<P>(&self, predicate: P) -> RcConditionalMutator<T>where
P: Predicate<T> + 'static,
Creates a conditional mutator that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the mutation operation
§Returns
Returns a conditional mutator that only executes when the
predicate returns true.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use prism3_rust_function::mutators::*;
let counter = Arc::new(AtomicI32::new(0));
let mutator = ArcMutator::new({
let counter = Arc::clone(&counter);
move |value: &mut i32| {
*value += counter.fetch_add(1, Ordering::SeqCst);
}
});
let conditional = mutator.when(|value: &i32| *value > 0);
let mut val = 1;
conditional.apply(&mut val); // val = 2 (1 + 1)
let mut val2 = -1;
conditional.apply(&mut val2); // not executed, val2 remains -1Sourcepub fn and_then<M>(&self, after: M) -> RcMutator<T>where
T: 'static,
M: Mutator<T> + 'static,
pub fn and_then<M>(&self, after: M) -> RcMutator<T>where
T: 'static,
M: Mutator<T> + 'static,
Chains execution with another mutator, executing the current mutator first, then the subsequent mutator.
§Parameters
after- The subsequent mutator to execute after the current mutator completes
§Returns
Returns a new mutator that executes the current mutator and the subsequent mutator in sequence.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use prism3_rust_function::mutators::*;
let counter1 = Arc::new(AtomicI32::new(0));
let counter2 = Arc::new(AtomicI32::new(0));
let mutator1 = ArcMutator::new({
let counter = Arc::clone(&counter1);
move |value: &mut i32| {
*value += counter.fetch_add(1, Ordering::SeqCst);
}
});
let mutator2 = ArcMutator::new({
let counter = Arc::clone(&counter2);
move |value: &mut i32| {
*value += counter.fetch_add(1, Ordering::SeqCst);
}
});
let chained = mutator1.and_then(mutator2);
let mut val = 0;
chained.apply(&mut val);
// val = 2 (0 + 1 + 1)Trait Implementations§
Source§impl<T> Mutator<T> for RcMutator<T>
impl<T> Mutator<T> for RcMutator<T>
Source§fn into_box(self) -> BoxMutator<T>where
T: 'static,
fn into_box(self) -> BoxMutator<T>where
T: 'static,
BoxMutator<T>. Read moreSource§fn into_rc(self) -> RcMutator<T>where
T: 'static,
fn into_rc(self) -> RcMutator<T>where
T: 'static,
RcMutator<T>. Read moreSource§fn into_fn(self) -> impl Fn(&mut T)
fn into_fn(self) -> impl Fn(&mut T)
Fn(&mut T) closure. Read moreSource§fn to_box(&self) -> BoxMutator<T>where
T: 'static,
fn to_box(&self) -> BoxMutator<T>where
T: 'static,
Source§fn into_once(self) -> BoxMutatorOnce<T>where
T: 'static,
fn into_once(self) -> BoxMutatorOnce<T>where
T: 'static,
BoxMutatorOnce<T> (consuming). Read moreSource§fn to_once(&self) -> BoxMutatorOnce<T>where
T: 'static,
fn to_once(&self) -> BoxMutatorOnce<T>where
T: 'static,
Source§fn into_arc(self) -> ArcMutator<T>
fn into_arc(self) -> ArcMutator<T>
ArcMutator<T>. Read more