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§
Provided Methods§
Sourcefn into_box(self) -> BoxStatefulFunction<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
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 * 2Sourcefn into_rc(self) -> RcStatefulFunction<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,
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 * 2Sourcefn into_arc(self) -> ArcStatefulFunction<T, R>
fn into_arc(self) -> ArcStatefulFunction<T, R>
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 * 2Sourcefn into_fn(self) -> impl FnMut(&T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn(self) -> impl FnMut(&T) -> Rwhere
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);Sourcefn into_once(self) -> BoxFunctionOnce<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,
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);Sourcefn to_box(&self) -> BoxStatefulFunction<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
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.
Sourcefn to_rc(&self) -> RcStatefulFunction<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,
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.
Sourcefn to_arc(&self) -> ArcStatefulFunction<T, R>
fn to_arc(&self) -> ArcStatefulFunction<T, R>
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.
Sourcefn to_fn(&self) -> impl FnMut(&T) -> R
fn to_fn(&self) -> impl FnMut(&T) -> R
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.
Sourcefn to_once(&self) -> BoxFunctionOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
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§
impl<F, T, R> StatefulFunction<T, R> for F
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