BoxStatefulMutatingFunction

Struct BoxStatefulMutatingFunction 

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

BoxStatefulMutatingFunction struct

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

§Features

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

§Use Cases

Choose BoxStatefulMutatingFunction when:

  • The function needs to maintain internal state
  • Building pipelines where ownership naturally flows
  • No need to share the function across contexts
  • Performance is critical and no sharing overhead is acceptable

§Performance

BoxStatefulMutatingFunction 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::{StatefulMutatingFunction,
                      BoxStatefulMutatingFunction};

let mut counter = {
    let mut count = 0;
    BoxStatefulMutatingFunction::new(move |x: &mut i32| {
        count += 1;
        *x *= 2;
        count
    })
};
let mut value = 5;
assert_eq!(counter.apply(&mut value), 1);
assert_eq!(value, 10);

§Author

Haixing Hu

Implementations§

Source§

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

Source

pub fn new<F>(f: F) -> Self
where F: FnMut(&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: FnMut(&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: FnMut(&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, ) -> BoxConditionalStatefulMutatingFunction<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) -> BoxStatefulMutatingFunction<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> BoxStatefulMutatingFunction<T, T>
where T: Clone + 'static,

Source

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

Creates an identity function

§Examples

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

Trait Implementations§

Source§

impl<T, R> Debug for BoxStatefulMutatingFunction<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 BoxStatefulMutatingFunction<T, R>

Source§

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

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

impl<T, R> StatefulMutatingFunction<T, R> for BoxStatefulMutatingFunction<T, R>

Source§

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

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

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

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

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

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

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

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

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

Convert to StatefulMutatingFunctionOnce Read more
Source§

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

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

Auto Trait Implementations§

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.