MutatorOnce

Trait MutatorOnce 

Source
pub trait MutatorOnce<T> {
    // Required methods
    fn mutate(self, value: &mut T);
    fn into_box(self) -> BoxMutatorOnce<T>
       where Self: Sized + '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:

  • Mutator uses &mut self, can be called multiple times
  • MutatorOnce uses self, can only be called once

§Features

  • Unified Interface: All one-time mutators share the same mutate method signature
  • Automatic Implementation: Closures automatically implement this trait with zero overhead
  • Type Conversions: Provides into_box method 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(&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();

§Author

Haixing Hu

Required Methods§

Source

fn mutate(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(&mut target);
assert_eq!(target, vec![0, 1, 2, 3]);
Source

fn into_box(self) -> BoxMutatorOnce<T>
where Self: Sized + 'static, T: 'static,

Converts to BoxMutatorOnce

⚠️ Consumes self: The original mutator becomes unavailable after calling this method.

Converts the current mutator to BoxMutatorOnce<T>.

§Ownership

This method consumes the mutator (takes ownership of self). After calling this method, the original mutator is no longer available.

§Returns

Returns the wrapped BoxMutatorOnce<T>

§Examples
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();

Implementors§

Source§

impl<T> MutatorOnce<T> for BoxConditionalMutatorOnce<T>
where T: 'static,

Source§

impl<T> MutatorOnce<T> for BoxMutatorOnce<T>

Source§

impl<T, F> MutatorOnce<T> for F
where F: FnOnce(&mut T),