pub struct ArcStatefulMutator<T> { /* private fields */ }Expand description
ArcMutator struct
A mutator implementation based on Arc<Mutex<dyn FnMut(&mut T) + Send>>
for thread-safe shared ownership scenarios. This type allows the mutator
to be safely shared and used across multiple threads.
§Features
- Shared Ownership: Cloneable via
Arc, multiple owners allowed - Thread-Safe: Implements
Send + Sync, safe for concurrent use - Interior Mutability: Uses
Mutexfor safe concurrent mutations - Mutable State: Can modify captured environment via
FnMut - Chainable: Method chaining via
&self(non-consuming)
§Use Cases
Choose ArcMutator when:
- The mutator needs to be shared across multiple threads
- Concurrent task processing (e.g., thread pools)
- Thread safety is required (Send + Sync)
§Examples
use prism3_function::{Mutator, ArcMutator};
let mutator = ArcMutator::new(|x: &mut i32| *x *= 2);
let clone = mutator.clone();
let mut value = 5;
let mut m = mutator;
m.mutate(&mut value);
assert_eq!(value, 10);§Author
Haixing Hu
Implementations§
Source§impl<T> ArcStatefulMutator<T>where
T: 'static,
impl<T> ArcStatefulMutator<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) -> ArcConditionalStatefulMutator<T>
pub fn when<P>(&self, predicate: P) -> ArcConditionalStatefulMutator<T>
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) -> ArcStatefulMutator<T>
pub fn and_then<M>(&self, after: M) -> ArcStatefulMutator<T>
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> Clone for ArcStatefulMutator<T>
impl<T> Clone for ArcStatefulMutator<T>
Source§impl<T> Debug for ArcStatefulMutator<T>
impl<T> Debug for ArcStatefulMutator<T>
Source§impl<T> Display for ArcStatefulMutator<T>
impl<T> Display for ArcStatefulMutator<T>
Source§impl<T> StatefulMutator<T> for ArcStatefulMutator<T>
impl<T> StatefulMutator<T> for ArcStatefulMutator<T>
Source§fn into_box(self) -> BoxStatefulMutator<T>where
T: 'static,
fn into_box(self) -> BoxStatefulMutator<T>where
T: 'static,
BoxMutator<T>. Read moreSource§fn into_rc(self) -> RcStatefulMutator<T>where
T: 'static,
fn into_rc(self) -> RcStatefulMutator<T>where
T: 'static,
RcMutator<T>. Read moreSource§fn into_arc(self) -> ArcStatefulMutator<T>where
T: 'static,
fn into_arc(self) -> ArcStatefulMutator<T>where
T: 'static,
ArcMutator<T>. Read moreSource§fn into_fn(self) -> impl FnMut(&mut T)
fn into_fn(self) -> impl FnMut(&mut T)
FnMut(&mut T) closure. Read moreSource§fn into_once(self) -> BoxMutatorOnce<T>where
T: 'static,
fn into_once(self) -> BoxMutatorOnce<T>where
T: 'static,
BoxMutatorOnce<T> (consuming). Read more