pub trait MutatorOnce<T> {
// Required method
fn mutate_once(self, value: &mut T);
// Provided methods
fn into_box_once(self) -> BoxMutatorOnce<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_fn_once(self) -> impl FnOnce(&mut T)
where Self: Sized + 'static,
T: 'static { ... }
fn to_box_once(&self) -> BoxMutatorOnce<T>
where Self: Sized + Clone + 'static,
T: 'static { ... }
fn to_fn_once(&self) -> impl FnOnce(&mut T)
where Self: Sized + Clone + 'static,
T: 'static { ... }
}Expand description
MutatorOnce trait - One-time mutator interface
Defines the core behavior of all one-time mutator types. Performs operations that consume self and modify the input value.
This trait is automatically implemented by:
- All closures implementing
FnOnce(&mut T) BoxMutatorOnce<T>
§Design Rationale
This trait provides a unified abstraction for one-time mutation operations.
The key difference from Mutator:
Mutatoruses&mut self, can be called multiple timesMutatorOnceusesself, can only be called once
§Features
- Unified Interface: All one-time mutators share the same
mutatemethod signature - Automatic Implementation: Closures automatically implement this trait with zero overhead
- Type Conversions: Provides
into_boxmethod for type conversion - Generic Programming: Write functions that work with any one-time mutator type
§Examples
§Generic Function
use prism3_function::{MutatorOnce, BoxMutatorOnce};
fn apply_once<M: MutatorOnce<Vec<i32>>>(
mutator: M,
initial: Vec<i32>
) -> Vec<i32> {
let mut val = initial;
mutator.mutate_once(&mut val);
val
}
let data = vec![1, 2, 3];
let mutator = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
x.extend(data);
});
let result = apply_once(mutator, vec![0]);
assert_eq!(result, vec![0, 1, 2, 3]);§Type Conversion
use prism3_function::MutatorOnce;
let data = vec![1, 2, 3];
let closure = move |x: &mut Vec<i32>| x.extend(data);
let box_mutator = closure.into_box_once();§Author
Haixing Hu
Required Methods§
Sourcefn mutate_once(self, value: &mut T)
fn mutate_once(self, value: &mut T)
Performs the one-time mutation operation
Consumes self and executes an operation on the given mutable reference. The operation typically modifies the input value or produces side effects, and can only be called once.
§Parameters
value- A mutable reference to the value to be mutated
§Examples
use prism3_function::{MutatorOnce, BoxMutatorOnce};
let data = vec![1, 2, 3];
let mutator = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
x.extend(data);
});
let mut target = vec![0];
mutator.mutate_once(&mut target);
assert_eq!(target, vec![0, 1, 2, 3]);Provided Methods§
Sourcefn into_box_once(self) -> BoxMutatorOnce<T>where
Self: Sized + 'static,
T: 'static,
fn into_box_once(self) -> BoxMutatorOnce<T>where
Self: Sized + 'static,
T: 'static,
Converts to BoxMutatorOnce (consuming)
Consumes self and returns an owned BoxMutatorOnce<T>. The default
implementation simply wraps the consuming mutate_once(self, &mut T) call
in a Box<dyn FnOnce(&mut T)>. Types that can provide a cheaper or
identity conversion (for example BoxMutatorOnce itself) should
override this method.
§Note
- This method consumes the source value.
- Implementors may return
selfdirectly whenSelfis already aBoxMutatorOnce<T>to avoid the extra wrapper allocation.
Sourcefn into_fn_once(self) -> impl FnOnce(&mut T)where
Self: Sized + 'static,
T: 'static,
fn into_fn_once(self) -> impl FnOnce(&mut T)where
Self: Sized + 'static,
T: 'static,
Converts to a consuming closure FnOnce(&mut T)
Consumes self and returns a closure that, when invoked, calls
mutate_once(self, &mut T). This is the default, straightforward
implementation; types that can produce a more direct function pointer
or avoid additional captures may override it.
Sourcefn to_box_once(&self) -> BoxMutatorOnce<T>
fn to_box_once(&self) -> BoxMutatorOnce<T>
Non-consuming adapter to BoxMutatorOnce
Creates a BoxMutatorOnce<T> that does not consume self. The default
implementation requires Self: Clone and clones the receiver for the
stored closure; the clone is consumed when the boxed mutator is invoked.
Types that can provide a zero-cost adapter (for example clonable
closures) should override this method to avoid unnecessary allocations.
Sourcefn to_fn_once(&self) -> impl FnOnce(&mut T)
fn to_fn_once(&self) -> impl FnOnce(&mut T)
Non-consuming adapter to a callable FnOnce(&mut T)
Returns a closure that does not consume self. The default requires
Self: Clone and clones self for the captured closure; the clone is
consumed when the returned closure is invoked. Implementors may provide
more efficient adapters for specific types.