pub trait MutatingFunction<T, R> {
// Required method
fn apply(&self, t: &mut T) -> R;
// Provided methods
fn into_box(self) -> BoxMutatingFunction<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn into_rc(self) -> RcMutatingFunction<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn into_arc(self) -> ArcMutatingFunction<T, R>
where Self: Sized + Send + Sync + 'static,
T: Send + 'static,
R: Send + 'static { ... }
fn into_fn(self) -> impl Fn(&mut T) -> R
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn into_once(self) -> BoxMutatingFunctionOnce<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn to_box(&self) -> BoxMutatingFunction<T, R>
where Self: Sized + Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_rc(&self) -> RcMutatingFunction<T, R>
where Self: Sized + Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_arc(&self) -> ArcMutatingFunction<T, R>
where Self: Sized + Clone + Send + Sync + 'static,
T: Send + 'static,
R: Send + 'static { ... }
fn to_fn(&self) -> impl Fn(&mut T) -> R
where Self: Sized + Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_once(&self) -> BoxMutatingFunctionOnce<T, R>
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
}Expand description
MutatingFunction trait - Unified mutating function interface
It is similar to the Fn(&mut T) -> R trait in the standard library.
Defines the core behavior of all mutating function types. Performs operations that accept a mutable reference, potentially modify it, and return a result.
This trait is automatically implemented by:
- All closures implementing
Fn(&mut T) -> R BoxMutatingFunction<T, R>,ArcMutatingFunction<T, R>, andRcMutatingFunction<T, R>
§Design Rationale
The trait provides a unified abstraction over different ownership models for operations that both modify input and return results. This is useful for scenarios where you need to:
- Update state and return information about the update
- Perform atomic-like operations (modify and return)
- Implement event handlers that modify state and signal continuation
§Features
- Unified Interface: All mutating function types share the same
applymethod signature - Automatic Implementation: Closures automatically implement this trait
- Type Conversions: Easy conversion between ownership models
- Generic Programming: Write functions that work with any mutating function type
§Examples
§Generic Function
use prism3_function::{MutatingFunction, BoxMutatingFunction};
fn apply_and_log<F: MutatingFunction<i32, i32>>(
func: &F,
value: i32
) -> i32 {
let mut val = value;
let result = func.apply(&mut val);
println!("Modified: {} -> {}, returned: {}", value, val, result);
result
}
let incrementer = BoxMutatingFunction::new(|x: &mut i32| {
*x += 1;
*x
});
assert_eq!(apply_and_log(&incrementer, 5), 6);§Type Conversion
use prism3_function::MutatingFunction;
let closure = |x: &mut i32| {
*x *= 2;
*x
};
// Convert to different ownership models
let box_func = closure.into_box();
// let rc_func = closure.into_rc(); // closure moved
// let arc_func = closure.into_arc(); // closure moved§Author
Haixing Hu
Required Methods§
Sourcefn apply(&self, t: &mut T) -> R
fn apply(&self, t: &mut T) -> R
Applies the function to the mutable reference and returns a result
Executes an operation on the given mutable reference, potentially modifying it, and returns a result value.
§Parameters
t- A mutable reference to the input value
§Returns
The computed result value
§Examples
use prism3_function::{MutatingFunction, BoxMutatingFunction};
let func = BoxMutatingFunction::new(|x: &mut i32| {
let old = *x;
*x += 1;
old
});
let mut value = 5;
let old_value = func.apply(&mut value);
assert_eq!(old_value, 5);
assert_eq!(value, 6);Provided Methods§
Sourcefn into_box(self) -> BoxMutatingFunction<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_box(self) -> BoxMutatingFunction<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
Convert this mutating function into a BoxMutatingFunction<T, R>.
This consuming conversion takes ownership of self and returns a
boxed implementation that forwards calls to the original function.
Types that can provide a more efficient conversion may override the
default implementation.
§Consumption
This method consumes the function: the original value will no longer
be available after the call. For cloneable functions call .clone()
before converting if you need to retain the original instance.
§Returns
A BoxMutatingFunction<T, R> that forwards to the original function.
§Examples
use prism3_function::MutatingFunction;
let closure = |x: &mut i32| {
*x *= 2;
*x
};
let mut boxed = closure.into_box();
let mut value = 5;
assert_eq!(boxed.apply(&mut value), 10);Sourcefn into_rc(self) -> RcMutatingFunction<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_rc(self) -> RcMutatingFunction<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
Convert this mutating function into an RcMutatingFunction<T, R>.
This consuming conversion takes ownership of self and returns an
Rc-backed function that forwards calls to the original. Override to
provide a more direct or efficient conversion when available.
§Consumption
This method consumes the function. If you need to keep the original instance, clone it prior to calling this method.
§Returns
An RcMutatingFunction<T, R> forwarding to the original function.
§Examples
use prism3_function::MutatingFunction;
let closure = |x: &mut i32| {
*x *= 2;
*x
};
let mut rc = closure.into_rc();
let mut value = 5;
assert_eq!(rc.apply(&mut value), 10);Sourcefn into_arc(self) -> ArcMutatingFunction<T, R>
fn into_arc(self) -> ArcMutatingFunction<T, R>
Convert this mutating function into an ArcMutatingFunction<T, R>.
This consuming conversion takes ownership of self and returns an
Arc-wrapped, thread-safe function. Types may override the default
implementation to provide a more efficient conversion.
§Consumption
This method consumes the function. Clone the instance first if you need to retain the original for further use.
§Returns
An ArcMutatingFunction<T, R> that forwards to the original
function.
§Examples
use prism3_function::MutatingFunction;
let closure = |x: &mut i32| {
*x *= 2;
*x
};
let mut arc = closure.into_arc();
let mut value = 5;
assert_eq!(arc.apply(&mut value), 10);Sourcefn into_fn(self) -> impl Fn(&mut T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn(self) -> impl Fn(&mut T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
Consume the function and return an Fn(&mut T) -> R closure.
The returned closure forwards calls to the original function and is suitable for use with iterator adapters or other contexts expecting closures.
§Consumption
This method consumes the function. The original instance will not be available after calling this method.
§Returns
A closure implementing Fn(&mut T) -> R which forwards to the
original function.
§Examples
use prism3_function::{MutatingFunction, BoxMutatingFunction};
let func = BoxMutatingFunction::new(|x: &mut i32| {
*x *= 2;
*x
});
let closure = func.into_fn();
let mut value = 5;
assert_eq!(closure(&mut value), 10);Sourcefn into_once(self) -> BoxMutatingFunctionOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_once(self) -> BoxMutatingFunctionOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
Convert to MutatingFunctionOnce
⚠️ Consumes self: The original function will be unavailable
after calling this method.
Converts a reusable mutating function to a one-time function that
consumes itself on use. This enables passing MutatingFunction to
functions that require MutatingFunctionOnce.
§Returns
Returns a BoxMutatingFunctionOnce<T, R>
§Examples
use prism3_function::{MutatingFunctionOnce, MutatingFunction,
BoxMutatingFunction};
fn takes_once<F: MutatingFunctionOnce<i32, i32>>(func: F, value: &mut i32) {
let result = func.apply(value);
println!("Result: {}", result);
}
let func = BoxMutatingFunction::new(|x: &mut i32| {
*x *= 2;
*x
});
let mut value = 5;
takes_once(func.into_once(), &mut value);Sourcefn to_box(&self) -> BoxMutatingFunction<T, R>
fn to_box(&self) -> BoxMutatingFunction<T, R>
Create a non-consuming BoxMutatingFunction<T, R> that forwards to
self.
The default implementation clones self (requires Clone) and
returns a boxed function that calls the cloned instance. Override this
method if a more efficient conversion exists.
§Returns
A BoxMutatingFunction<T, R> that forwards to a clone of self.
Sourcefn to_rc(&self) -> RcMutatingFunction<T, R>
fn to_rc(&self) -> RcMutatingFunction<T, R>
Create a non-consuming RcMutatingFunction<T, R> that forwards to
self.
The default implementation clones self (requires Clone) and
returns an Rc-backed function that forwards calls to the clone.
Override to provide a more direct or efficient conversion if needed.
§Returns
An RcMutatingFunction<T, R> that forwards to a clone of self.
Sourcefn to_arc(&self) -> ArcMutatingFunction<T, R>
fn to_arc(&self) -> ArcMutatingFunction<T, R>
Create a non-consuming ArcMutatingFunction<T, R> that forwards to
self.
The default implementation clones self (requires
Clone + Send + Sync) and returns an Arc-wrapped function that
forwards calls to the clone. Override when a more efficient conversion
is available.
§Returns
An ArcMutatingFunction<T, R> that forwards to a clone of self.
Sourcefn to_fn(&self) -> impl Fn(&mut T) -> R
fn to_fn(&self) -> impl Fn(&mut T) -> R
Create a boxed Fn(&mut T) -> R 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) -> R which forwards to the
original function.
Sourcefn to_once(&self) -> BoxMutatingFunctionOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_once(&self) -> BoxMutatingFunctionOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Convert to MutatingFunctionOnce without consuming self
⚠️ Requires Clone: This method requires Self to implement Clone.
Clones the current function and converts the clone to a one-time function.
§Returns
Returns a BoxMutatingFunctionOnce<T, R>
§Examples
use prism3_function::{MutatingFunctionOnce, MutatingFunction,
BoxMutatingFunction};
fn takes_once<F: MutatingFunctionOnce<i32, i32>>(func: F, value: &mut i32) {
let result = func.apply(value);
println!("Result: {}", result);
}
let func = BoxMutatingFunction::new(|x: &mut i32| {
*x *= 2;
*x
});
let mut value = 5;
takes_once(func.to_once(), &mut value);