StatefulMutator

Trait StatefulMutator 

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

    // Provided methods
    fn into_box(self) -> BoxStatefulMutator<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_rc(self) -> RcStatefulMutator<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_arc(self) -> ArcStatefulMutator<T>
       where Self: Sized + Send + 'static,
             T: Send + 'static { ... }
    fn into_fn(self) -> impl FnMut(&mut T)
       where Self: Sized + 'static,
             T: 'static { ... }
    fn to_box(&self) -> BoxStatefulMutator<T>
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn to_rc(&self) -> RcStatefulMutator<T>
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn to_arc(&self) -> ArcStatefulMutator<T>
       where Self: Sized + Clone + Send + 'static,
             T: Send + 'static { ... }
    fn to_fn(&self) -> impl FnMut(&mut T)
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn into_once(self) -> BoxMutatorOnce<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn to_once(&self) -> BoxMutatorOnce<T>
       where Self: Sized + Clone + '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.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(&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.apply(&mut value);
assert_eq!(value, 10);

Provided Methods§

Source

fn into_box(self) -> BoxStatefulMutator<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) -> RcStatefulMutator<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) -> ArcStatefulMutator<T>
where Self: Sized + Send + '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 FnMut(&mut T)
where Self: Sized + 'static, T: 'static,

Consume the mutator and return an FnMut(&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 FnMut(&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 to_box(&self) -> BoxStatefulMutator<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) -> RcStatefulMutator<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) -> ArcStatefulMutator<T>
where Self: Sized + Clone + Send + 'static, T: Send + 'static,

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

The default implementation clones self (requires Clone + Send) 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 FnMut(&mut T)
where Self: Sized + Clone + 'static, T: 'static,

Create a boxed FnMut(&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 FnMut(&mut T) which forwards to the original mutator.

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::{StatefulMutator, BoxStatefulMutator,
                      BoxMutatorOnce};

let mutator = BoxStatefulMutator::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_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> StatefulMutator<T> for ArcConditionalStatefulMutator<T>
where T: Send + 'static,

Source§

impl<T> StatefulMutator<T> for ArcStatefulMutator<T>

Source§

impl<T> StatefulMutator<T> for BoxConditionalStatefulMutator<T>
where T: 'static,

Source§

impl<T> StatefulMutator<T> for BoxStatefulMutator<T>

Source§

impl<T> StatefulMutator<T> for RcConditionalStatefulMutator<T>
where T: 'static,

Source§

impl<T> StatefulMutator<T> for RcStatefulMutator<T>

Source§

impl<T, F> StatefulMutator<T> for F
where F: FnMut(&mut T), T: 'static,