pub struct BoxStatefulMutator<T> { /* private fields */ }Expand description
BoxMutator struct
A mutator implementation based on Box<dyn FnMut(&mut T)> for single
ownership scenarios. This is the simplest and most efficient mutator
type when sharing is not required.
§Features
- Single Ownership: Not cloneable, ownership moves on use
- Zero Overhead: No reference counting or locking
- Mutable State: Can modify captured environment via
FnMut - Builder Pattern: Method chaining consumes
selfnaturally - Factory Methods: Convenient constructors for common patterns
§Use Cases
Choose BoxMutator when:
- The mutator is used only once or in a linear flow
- Building pipelines where ownership naturally flows
- No need to share the mutator across contexts
- Performance is critical and no sharing overhead is acceptable
§Performance
BoxMutator has the best performance among the three mutator types:
- No reference counting overhead
- No lock acquisition or runtime borrow checking
- Direct function call through vtable
- Minimal memory footprint (single pointer)
§Examples
use prism3_function::{Mutator, BoxMutator};
let mut mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
let mut value = 5;
mutator.apply(&mut value);
assert_eq!(value, 10);§Author
Haixing Hu
Implementations§
Source§impl<T> BoxStatefulMutator<T>where
T: 'static,
impl<T> BoxStatefulMutator<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) -> BoxConditionalStatefulMutator<T>where
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalStatefulMutator<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 = BoxMutator::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) -> BoxStatefulMutator<T>where
Self: Sized + 'static,
T: 'static,
M: StatefulMutator<T> + 'static,
pub fn and_then<M>(self, after: M) -> BoxStatefulMutator<T>where
Self: Sized + 'static,
T: 'static,
M: StatefulMutator<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 = BoxMutator::new({
let counter = Arc::clone(&counter1);
move |value: &mut i32| {
*value += counter.fetch_add(1, Ordering::SeqCst);
}
});
let mutator2 = BoxMutator::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> Debug for BoxStatefulMutator<T>
impl<T> Debug for BoxStatefulMutator<T>
Source§impl<T> Display for BoxStatefulMutator<T>
impl<T> Display for BoxStatefulMutator<T>
Source§impl<T> StatefulMutator<T> for BoxStatefulMutator<T>
impl<T> StatefulMutator<T> for BoxStatefulMutator<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_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