BoxMutatingFunction

Struct BoxMutatingFunction 

Source
pub struct BoxMutatingFunction<T, R> { /* private fields */ }
Expand description

BoxMutatingFunction struct

A mutating function implementation based on Box<dyn Fn(&mut T) -> R> for single ownership scenarios. This is the simplest and most efficient mutating function type when sharing is not required.

§Features

  • Single Ownership: Not cloneable, ownership moves on use
  • Zero Overhead: No reference counting or locking
  • Stateless: Cannot modify captured environment (uses Fn not FnMut)
  • Builder Pattern: Method chaining consumes self naturally
  • Factory Methods: Convenient constructors for common patterns

§Use Cases

Choose BoxMutatingFunction when:

  • The function is used for stateless operations
  • Building pipelines where ownership naturally flows
  • No need to share the function across contexts
  • Performance is critical and no sharing overhead is acceptable

§Performance

BoxMutatingFunction has the best performance among the three function types:

  • No reference counting overhead
  • No lock acquisition or runtime borrow checking
  • Direct function call through vtable
  • Minimal memory footprint (single pointer)

§Examples

use prism3_function::{MutatingFunction, BoxMutatingFunction};

let func = BoxMutatingFunction::new(|x: &mut i32| {
    *x *= 2;
    *x
});
let mut value = 5;
assert_eq!(func.apply(&mut value), 10);
assert_eq!(value, 10);

§Author

Haixing Hu

Implementations§

Source§

impl<T, R> BoxMutatingFunction<T, R>
where T: 'static, R: 'static,

Source

pub fn new<F>(f: F) -> Self
where F: Fn(&mut T) -> R + 'static,

Creates a new function.

Wraps the provided closure in the appropriate smart pointer type for this function implementation.

Source

pub fn new_with_name<F>(name: &str, f: F) -> Self
where F: Fn(&mut T) -> R + 'static,

Creates a new named function.

Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: Fn(&mut T) -> R + 'static,

Creates a new named function with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this function.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this function.

§Parameters
  • name - The name to set for this function
Source

pub fn when<P>(self, predicate: P) -> BoxConditionalMutatingFunction<T, R>
where P: Predicate<T> + 'static,

Creates a conditional function that executes based on predicate result.

§Parameters
  • predicate - The predicate to determine whether to execute the function operation
§Returns

Returns a conditional function that only executes when the predicate returns true.

§Examples
use prism3_function::{BoxFunction, Function};

let double = BoxFunction::new(|x: i32| x * 2);
let conditional = double.when(|value: &i32| *value > 0);
assert_eq!(conditional.or_else(|_| 0).apply(5), 10);  // executed
assert_eq!(conditional.or_else(|_| 0).apply(-3), 0);  // not executed
Source

pub fn and_then<S, F>(self, after: F) -> BoxMutatingFunction<T, S>
where S: 'static, F: Function<R, S> + 'static,

Chains execution with another function, executing the current function first, then the subsequent function.

§Parameters
  • after - The subsequent function to execute after the current function completes
§Returns

Returns a new function that executes the current function and the subsequent function in sequence.

§Examples
use prism3_function::{BoxFunction, Function};

let double = BoxFunction::new(|x: i32| x * 2);
let to_string = BoxFunction::new(|x: i32| x.to_string());

let chained = double.and_then(to_string);
assert_eq!(chained.apply(5), "10".to_string());
Source§

impl<T> BoxMutatingFunction<T, T>
where T: Clone + 'static,

Source

pub fn identity() -> BoxMutatingFunction<T, T>

Creates an identity function

§Examples

/// rust /// use prism3_function::BoxMutatingFunction; /// /// let mut identity = BoxMutatingFunction::<i32, i32>::identity(); /// let mut value = 42; /// assert_eq!(identity.apply(&mut value), 42); ///

Trait Implementations§

Source§

impl<T, R> Debug for BoxMutatingFunction<T, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, R> Display for BoxMutatingFunction<T, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, R> MutatingFunction<T, R> for BoxMutatingFunction<T, R>

Source§

fn apply(&self, t: &mut T) -> R

Applies the function to the mutable reference and returns a result Read more
Source§

fn into_box(self) -> BoxMutatingFunction<T, R>
where T: 'static, R: 'static,

Convert this mutating function into a BoxMutatingFunction<T, R>. Read more
Source§

fn into_rc(self) -> RcMutatingFunction<T, R>
where T: 'static, R: 'static,

Convert this mutating function into an RcMutatingFunction<T, R>. Read more
Source§

fn into_fn(self) -> impl Fn(&mut T) -> R

Consume the function and return an Fn(&mut T) -> R closure. Read more
Source§

fn into_once(self) -> BoxMutatingFunctionOnce<T, R>
where T: 'static, R: 'static,

Convert to MutatingFunctionOnce Read more
Source§

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

Convert this mutating function into an ArcMutatingFunction<T, R>. Read more

Auto Trait Implementations§

§

impl<T, R> Freeze for BoxMutatingFunction<T, R>

§

impl<T, R> !RefUnwindSafe for BoxMutatingFunction<T, R>

§

impl<T, R> !Send for BoxMutatingFunction<T, R>

§

impl<T, R> !Sync for BoxMutatingFunction<T, R>

§

impl<T, R> Unpin for BoxMutatingFunction<T, R>

§

impl<T, R> !UnwindSafe for BoxMutatingFunction<T, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.