Mutator

Trait Mutator 

Source
pub trait Mutator<T> {
    // Required methods
    fn mutate(&mut self, value: &mut T);
    fn into_box(self) -> BoxMutator<T>
       where Self: Sized + 'static,
             T: 'static;
    fn into_rc(self) -> RcMutator<T>
       where Self: Sized + 'static,
             T: 'static;
    fn into_arc(self) -> ArcMutator<T>
       where Self: Sized + Send + 'static,
             T: Send + 'static;
    fn into_fn(self) -> impl FnMut(&mut T)
       where Self: Sized + 'static,
             T: 'static;
}
Expand description

Mutator trait - Unified mutator interface

Defines the core behavior of all mutator types. Performs operations that accept a mutable reference and modify the input value (not just side effects).

This trait is automatically implemented by:

  • All closures implementing FnMut(&mut T)
  • BoxMutator<T>, ArcMutator<T>, and RcMutator<T>

§Design Rationale

The trait provides a unified abstraction over different ownership models, allowing generic code to work with any mutator type. Type conversion methods (into_box, into_arc, into_rc) enable flexible ownership transitions based on usage requirements.

§Features

  • Unified Interface: All mutator types share the same mutate method signature
  • Automatic Implementation: Closures automatically implement this trait with zero overhead
  • Type Conversions: Easy conversion between ownership models
  • Generic Programming: Write functions that work with any mutator type

§Examples

§Generic Mutator Function

use prism3_function::{Mutator, BoxMutator, ArcMutator};

fn apply_mutator<M: Mutator<i32>>(
    mutator: &mut M,
    value: i32
) -> i32 {
    let mut val = value;
    mutator.mutate(&mut val);
    val
}

// Works with any mutator type
let mut box_mut = BoxMutator::new(|x: &mut i32| *x *= 2);
assert_eq!(apply_mutator(&mut box_mut, 5), 10);

let mut arc_mut = ArcMutator::new(|x: &mut i32| *x *= 2);
assert_eq!(apply_mutator(&mut arc_mut, 5), 10);

let mut closure = |x: &mut i32| *x *= 2;
assert_eq!(apply_mutator(&mut closure, 5), 10);

§Type Conversion

use prism3_function::Mutator;

let closure = |x: &mut i32| *x *= 2;

// Convert to different ownership models
let box_mutator = closure.into_box();
// let rc_mutator = closure.into_rc();  // closure moved
// let arc_mutator = closure.into_arc(); // closure moved

§Author

Haixing Hu

Required Methods§

Source

fn mutate(&mut self, value: &mut T)

Performs the mutation operation

Executes an operation on the given mutable reference. The operation typically modifies the input value or produces side effects.

§Parameters
  • value - A mutable reference to the value to be mutated
§Examples
use prism3_function::{Mutator, BoxMutator};

let mut mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
let mut value = 5;
mutator.mutate(&mut value);
assert_eq!(value, 10);
Source

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

Converts to BoxMutator

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

Converts the current mutator to BoxMutator<T>.

§Ownership

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

Tip: For cloneable mutators (ArcMutator, RcMutator), you can call .clone() first if you need to keep the original.

§Returns

Returns the wrapped BoxMutator<T>

§Examples
§Basic Conversion
use prism3_function::Mutator;

let closure = |x: &mut i32| *x *= 2;
let mut box_mutator = closure.into_box();
let mut value = 5;
box_mutator.mutate(&mut value);
assert_eq!(value, 10);
Source

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

Converts to RcMutator

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

§Returns

Returns the wrapped RcMutator<T>

§Examples
use prism3_function::Mutator;

let closure = |x: &mut i32| *x *= 2;
let mut rc_mutator = closure.into_rc();
let mut value = 5;
rc_mutator.mutate(&mut value);
assert_eq!(value, 10);
Source

fn into_arc(self) -> ArcMutator<T>
where Self: Sized + Send + 'static, T: Send + 'static,

Converts to ArcMutator

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

§Returns

Returns the wrapped ArcMutator<T>

§Examples
use prism3_function::Mutator;

let closure = |x: &mut i32| *x *= 2;
let mut arc_mutator = closure.into_arc();
let mut value = 5;
arc_mutator.mutate(&mut value);
assert_eq!(value, 10);
Source

fn into_fn(self) -> impl FnMut(&mut T)
where Self: Sized + 'static, T: 'static,

Converts mutator to a closure for use with iterator methods

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

This method consumes the mutator and returns a closure that can be directly used with iterator methods like for_each().

§Returns

Returns a closure that implements FnMut(&mut T)

§Examples
use prism3_function::{Mutator, BoxMutator};

let mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
let mut values = vec![1, 2, 3, 4, 5];

values.iter_mut().for_each(mutator.into_fn());

assert_eq!(values, vec![2, 4, 6, 8, 10]);

Implementors§

Source§

impl<T> Mutator<T> for ArcConditionalMutator<T>
where T: Send + 'static,

Source§

impl<T> Mutator<T> for ArcMutator<T>

Source§

impl<T> Mutator<T> for BoxConditionalMutator<T>
where T: 'static,

Source§

impl<T> Mutator<T> for BoxMutator<T>

Source§

impl<T> Mutator<T> for RcConditionalMutator<T>
where T: 'static,

Source§

impl<T> Mutator<T> for RcMutator<T>

Source§

impl<T, F> Mutator<T> for F
where F: FnMut(&mut T),