StatefulFunction

Trait StatefulFunction 

Source
pub trait StatefulFunction<T, R> {
    // Required method
    fn apply(&mut self, t: &T) -> R;

    // Provided methods
    fn into_box(self) -> BoxStatefulFunction<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_rc(self) -> RcStatefulFunction<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_arc(self) -> ArcStatefulFunction<T, R>
       where Self: Sized + Send + Sync + 'static,
             T: Send + Sync + 'static,
             R: 'static { ... }
    fn into_fn(self) -> impl FnMut(&T) -> R
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_once(self) -> BoxFunctionOnce<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_box(&self) -> BoxStatefulFunction<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_rc(&self) -> RcStatefulFunction<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_arc(&self) -> ArcStatefulFunction<T, R>
       where Self: Clone + Send + Sync + 'static,
             T: Send + Sync + 'static,
             R: Send + 'static { ... }
    fn to_fn(&self) -> impl FnMut(&T) -> R
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_once(&self) -> BoxFunctionOnce<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

StatefulFunction trait - transforms values from type T to type R with state

Defines the behavior of a stateful transformation: converting a value of type T to a value of type R by consuming the input while allowing modification of internal state. This is analogous to FnMut(&T) -> R in Rust’s standard library.

§Type Parameters

  • T - The type of the input value (consumed)
  • R - The type of the output value

§Author

Haixing Hu

Required Methods§

Source

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

Applies the mapping to the input value to produce an output value

§Parameters
  • t - The input value to transform (consumed)
§Returns

The transformed output value

Provided Methods§

Source

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

Converts to BoxStatefulFunction

⚠️ Consumes self: The original stateful function becomes unavailable after calling this method.

§Returns

Returns BoxStatefulFunction<T, R>

§Default Implementation

The default implementation wraps self in a BoxStatefulFunction by creating a new closure that calls self.apply(). This provides a zero-cost abstraction for most use cases.

§Examples
use prism3_function::{StatefulFunction, BoxStatefulFunction};

struct CustomStatefulFunction {
    multiplier: i32,
}

impl StatefulFunction<i32, i32> for CustomStatefulFunction {
    fn apply(&mut self, input: i32) -> i32 {
        self.multiplier += 1;
        input * self.multiplier
    }
}

let function = CustomStatefulFunction { multiplier: 0 };
let mut boxed = function.into_box();
assert_eq!(boxed.apply(10), 10);  // 10 * 1
assert_eq!(boxed.apply(10), 20);  // 10 * 2
Source

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

Converts to RcStatefulFunction

⚠️ Consumes self: The original stateful function becomes unavailable after calling this method.

§Returns

Returns RcStatefulFunction<T, R>

§Default Implementation

The default implementation first converts to BoxStatefulFunction using into_box(), then wraps it in RcStatefulFunction. Specific implementations may override this for better efficiency.

§Examples
use prism3_function::{StatefulFunction, RcStatefulFunction};

struct CustomStatefulFunction {
    multiplier: i32,
}

impl StatefulFunction<i32, i32> for CustomStatefulFunction {
    fn apply(&mut self, input: i32) -> i32 {
        self.multiplier += 1;
        input * self.multiplier
    }
}

let function = CustomStatefulFunction { multiplier: 0 };
let mut rc_function = function.into_rc();
assert_eq!(rc_function.apply(10), 10);  // 10 * 1
assert_eq!(rc_function.apply(10), 20);  // 10 * 2
Source

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

Converts to ArcStatefulFunction

⚠️ Consumes self: The original stateful function becomes unavailable after calling this method.

§Returns

Returns ArcStatefulFunction<T, R>

§Default Implementation

The default implementation wraps self in an ArcStatefulFunction by creating a new closure that calls self.apply(). Note that this requires self to implement Send due to Arc’s thread-safety requirements.

§Examples
use prism3_function::{StatefulFunction, ArcStatefulFunction};

struct CustomStatefulFunction {
    multiplier: i32,
}

impl StatefulFunction<i32, i32> for CustomStatefulFunction {
    fn apply(&mut self, input: i32) -> i32 {
        self.multiplier += 1;
        input * self.multiplier
    }
}

let function = CustomStatefulFunction { multiplier: 0 };
let mut arc_function = function.into_arc();
assert_eq!(arc_function.apply(10), 10);  // 10 * 1
assert_eq!(arc_function.apply(10), 20);  // 10 * 2
Source

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

Converts to a closure implementing FnMut(&T) -> R

⚠️ Consumes self: The original stateful function becomes unavailable after calling this method.

§Returns

Returns an implementation of FnMut(&T) -> R

§Default Implementation

The default implementation creates a new closure that calls self.apply(). Specific implementations may override this for better efficiency.

§Examples
use prism3_function::{StatefulFunction, BoxStatefulFunction};

let function = BoxStatefulFunction::new(|x: i32| x * 2);
let mut closure = function.into_fn();
assert_eq!(closure(10), 20);
assert_eq!(closure(15), 30);
Source

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

Convert to StatefulFunctionOnce

⚠️ Consumes self: The original function will be unavailable after calling this method.

Converts a reusable stateful function to a one-time function that consumes itself on use. This enables passing StatefulFunction to functions that require StatefulFunctionOnce.

§Returns

Returns a BoxFunctionOnce<T, R>

§Examples
use prism3_function::{StatefulFunctionOnce, StatefulFunction,
                      BoxStatefulFunction};

fn takes_once<F: StatefulFunctionOnce<i32, i32>>(func: F, value: &i32) {
    let result = func.apply(value);
    println!("Result: {}", result);
}

let func = BoxStatefulFunction::new(|x: &i32| x * 2);
takes_once(func.into_once(), &5);
Source

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

Non-consuming conversion to BoxStatefulFunction.

Default implementation requires Self: Clone and wraps a cloned instance in a RefCell so the returned stateful function can mutate state across calls.

Source

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

Non-consuming conversion to RcStatefulFunction.

Default implementation clones self into an Rc<RefCell<_>> so the resulting stateful function can be shared within a single thread.

Source

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

Non-consuming conversion to ArcStatefulFunction (thread-safe).

Default implementation requires Self: Clone + Send + Sync and wraps the cloned instance in Arc<Mutex<_>> so it can be used across threads.

Source

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

Non-consuming conversion to a closure (FnMut(&T) -> R).

Default implementation clones self into a RefCell and returns a closure that calls apply on the interior mutable value.

Source

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

Convert to StatefulFunctionOnce 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 BoxFunctionOnce<T, R>

§Examples
use prism3_function::{StatefulFunctionOnce, StatefulFunction,
                      BoxStatefulFunction};

fn takes_once<F: StatefulFunctionOnce<i32, i32>>(func: F, value: &i32) {
    let result = func.apply(value);
    println!("Result: {}", result);
}

let func = BoxStatefulFunction::new(|x: &i32| x * 2);
takes_once(func.to_once(), &5);

Implementors§

Source§

impl<F, T, R> StatefulFunction<T, R> for F
where F: FnMut(&T) -> R, T: 'static, R: 'static,

Implement StatefulFunction<T, R> for any type that implements FnMut(&T) -> R

This allows closures to be used directly with our StatefulFunction trait without wrapping.

§Examples

use prism3_function::StatefulFunction;

let mut counter = 0;
let mut function = |x: i32| {
    counter += 1;
    x + counter
};

assert_eq!(function.apply(10), 11);
assert_eq!(function.apply(10), 12);

§Author

Haixing Hu

Source§

impl<T, R> StatefulFunction<T, R> for ArcStatefulFunction<T, R>

Source§

impl<T, R> StatefulFunction<T, R> for RcStatefulFunction<T, R>

Source§

impl<T: 'static, R: 'static> StatefulFunction<T, R> for BoxStatefulFunction<T, R>