Mutator

Trait Mutator 

Source
pub trait Mutator<T> {
    // Required method
    fn apply(&self, value: &mut T);

    // Provided methods
    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 + Sync + 'static,
             T: Send + 'static { ... }
    fn into_fn(self) -> impl Fn(&mut T)
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_once(self) -> BoxMutatorOnce<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn to_box(&self) -> BoxMutator<T>
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn to_rc(&self) -> RcMutator<T>
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn to_arc(&self) -> ArcMutator<T>
       where Self: Sized + Clone + Send + Sync + 'static,
             T: Send + 'static { ... }
    fn to_fn(&self) -> impl Fn(&mut T)
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn to_once(&self) -> BoxMutatorOnce<T>
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
}
Expand description

Mutator trait - Unified stateless mutator interface

Defines the core behavior of all stateless mutator types. Performs operations that accept a mutable reference and modify the input value without maintaining internal state.

This trait is automatically implemented by:

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

§Design Rationale

The trait provides a unified abstraction over different ownership models for stateless operations. Unlike StatefulMutator which uses FnMut and can modify its internal state, Mutator uses Fn for pure transformations.

§Features

  • Stateless Operations: No internal state modification (&self not &mut self)
  • Unified Interface: All mutator types share the same mutate method signature
  • Automatic Implementation: Closures automatically implement this trait
  • 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.apply(&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 apply(&self, value: &mut T)

Performs the stateless mutation operation

Executes an operation on the given mutable reference without modifying the mutator’s internal state. This is a pure transformation operation.

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

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

Provided Methods§

Source

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

Convert this mutator into a BoxMutator<T>.

This consuming conversion takes ownership of self and returns a boxed implementation that forwards calls to the original mutator. Types that can provide a more efficient conversion may override the default implementation.

§Consumption

This method consumes the mutator: the original value will no longer be available after the call. For cloneable mutators call .clone() before converting if you need to retain the original instance.

§Returns

A BoxMutator<T> that forwards to the original mutator.

§Examples
use prism3_function::Mutator;

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

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

Convert this mutator into an RcMutator<T>.

This consuming conversion takes ownership of self and returns an Rc-backed mutator that forwards calls to the original. Override to provide a more direct or efficient conversion when available.

§Consumption

This method consumes the mutator. If you need to keep the original instance, clone it prior to calling this method.

§Returns

An RcMutator<T> forwarding to the original mutator.

§Examples
use prism3_function::Mutator;

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

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

Convert this mutator into an ArcMutator<T>.

This consuming conversion takes ownership of self and returns an Arc-wrapped, thread-safe mutator. Types may override the default implementation to provide a more efficient conversion.

§Consumption

This method consumes the mutator. Clone the instance first if you need to retain the original for further use.

§Returns

An ArcMutator<T> that forwards to the original mutator.

§Examples
use prism3_function::Mutator;

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

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

Consume the mutator and return an Fn(&mut T) closure.

The returned closure forwards calls to the original mutator and is suitable for use with iterator adapters such as for_each.

§Consumption

This method consumes the mutator. The original instance will not be available after calling this method.

§Returns

A closure implementing Fn(&mut T) which forwards to the original mutator.

§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]);
Source

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

Convert this mutator into a BoxMutatorOnce<T> (consuming).

This consuming conversion takes ownership of self and returns a boxed one-time mutator that forwards calls to the original mutator. The returned mutator can only be used once.

§Consumption

This method consumes the mutator: the original value will no longer be available after the call. For cloneable mutators call .clone() before converting if you need to retain the original instance.

§Returns

A BoxMutatorOnce<T> that forwards to the original mutator.

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

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

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

Create a non-consuming BoxMutator<T> that forwards to self.

The default implementation clones self (requires Clone) and returns a boxed mutator that calls the cloned instance. Override this method if a more efficient conversion exists.

§Returns

A BoxMutator<T> that forwards to a clone of self.

Source

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

Create a non-consuming RcMutator<T> that forwards to self.

The default implementation clones self (requires Clone) and returns an Rc-backed mutator that forwards calls to the clone. Override to provide a more direct or efficient conversion if needed.

§Returns

An RcMutator<T> that forwards to a clone of self.

Source

fn to_arc(&self) -> ArcMutator<T>
where Self: Sized + Clone + Send + Sync + 'static, T: Send + 'static,

Create a non-consuming ArcMutator<T> that forwards to self.

The default implementation clones self (requires Clone + Send + Sync) and returns an Arc-wrapped mutator that forwards calls to the clone. Override when a more efficient conversion is available.

§Returns

An ArcMutator<T> that forwards to a clone of self.

Source

fn to_fn(&self) -> impl Fn(&mut T)
where Self: Sized + Clone + 'static, T: 'static,

Create a boxed Fn(&mut T) closure that forwards to self.

The default implementation clones self (requires Clone) and returns a boxed closure that invokes the cloned instance. Override to provide a more efficient conversion when possible.

§Returns

A closure implementing Fn(&mut T) which forwards to the original mutator.

Source

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

Create a non-consuming BoxMutatorOnce<T> that forwards to self.

The default implementation clones self (requires Clone) and returns a boxed one-time mutator that calls the cloned instance. Override this method if a more efficient conversion exists.

§Returns

A BoxMutatorOnce<T> that forwards to a clone of self.

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: Fn(&mut T), T: 'static,