pub struct RcStatefulFunction<T, R> { /* private fields */ }Expand description
RcStatefulFunction - single-threaded function wrapper
A single-threaded, clonable function wrapper optimized for scenarios that require sharing without thread-safety overhead.
§Features
- Based on:
Rc<RefCell<dyn FnMut(&T) -> R>> - Ownership: Shared ownership via reference counting (non-atomic)
- Reusability: Can be called multiple times (each call consumes its input)
- Thread Safety: Not thread-safe (no
Send + Sync) - Clonable: Cheap cloning via
Rc::clone - Statefulness: Can modify internal state between calls
§Author
Haixing Hu
Implementations§
Source§impl<T, R> RcStatefulFunction<T, R>where
T: 'static,
R: 'static,
impl<T, R> RcStatefulFunction<T, R>where
T: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new function.
Wraps the provided closure in the appropriate smart pointer type for this function implementation.
Sourcepub fn new_with_name<F>(name: &str, f: F) -> Self
pub fn new_with_name<F>(name: &str, f: F) -> Self
Creates a new named function.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
Creates a new named function with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn when<P>(&self, predicate: P) -> RcConditionalStatefulFunction<T, R>where
P: Predicate<T> + 'static,
pub fn when<P>(&self, predicate: P) -> RcConditionalStatefulFunction<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::{ArcFunction, Function};
use std::sync::Arc;
let double = ArcFunction::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 executedSourcepub fn and_then<S, F>(&self, after: F) -> RcStatefulFunction<T, S>where
S: 'static,
F: StatefulFunction<R, S> + 'static,
pub fn and_then<S, F>(&self, after: F) -> RcStatefulFunction<T, S>where
S: 'static,
F: StatefulFunction<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::{ArcFunction, Function};
use std::sync::Arc;
let double = ArcFunction::new(|x: i32| x * 2);
let to_string = ArcFunction::new(|x: i32| x.to_string());
let chained = double.and_then(to_string);
assert_eq!(chained.apply(5), "10".to_string());Source§impl<T, R> RcStatefulFunction<T, R>where
T: 'static,
R: Clone + 'static,
impl<T, R> RcStatefulFunction<T, R>where
T: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> RcStatefulFunction<T, R>
pub fn constant(value: R) -> RcStatefulFunction<T, R>
Creates a constant function
§Examples
/// rust /// use prism3_function::{RcStatefulFunction, Function}; /// /// let constant = RcStatefulFunction::constant("hello"); /// assert_eq!(constant.apply(123), "hello"); ///
Source§impl<T> RcStatefulFunction<T, T>where
T: Clone + 'static,
impl<T> RcStatefulFunction<T, T>where
T: Clone + 'static,
Sourcepub fn identity() -> RcStatefulFunction<T, T>
pub fn identity() -> RcStatefulFunction<T, T>
Creates an identity function
§Examples
/// rust /// use prism3_function::RcStatefulFunction; /// /// let identity = RcStatefulFunction::<i32, i32>::identity(); /// assert_eq!(identity.apply(&42), 42); ///
Trait Implementations§
Source§impl<T, R> Clone for RcStatefulFunction<T, R>
impl<T, R> Clone for RcStatefulFunction<T, R>
Source§impl<T, R> Debug for RcStatefulFunction<T, R>
impl<T, R> Debug for RcStatefulFunction<T, R>
Source§impl<T, R> Display for RcStatefulFunction<T, R>
impl<T, R> Display for RcStatefulFunction<T, R>
Source§impl<T, R> StatefulFunction<T, R> for RcStatefulFunction<T, R>
impl<T, R> StatefulFunction<T, R> for RcStatefulFunction<T, R>
Source§fn apply(&mut self, t: &T) -> R
fn apply(&mut self, t: &T) -> R
Source§fn into_box(self) -> BoxStatefulFunction<T, R>where
T: 'static,
R: 'static,
fn into_box(self) -> BoxStatefulFunction<T, R>where
T: 'static,
R: 'static,
Source§fn into_rc(self) -> RcStatefulFunction<T, R>where
T: 'static,
R: 'static,
fn into_rc(self) -> RcStatefulFunction<T, R>where
T: 'static,
R: 'static,
Source§fn into_fn(self) -> impl FnMut(&T) -> R
fn into_fn(self) -> impl FnMut(&T) -> R
FnMut(&T) -> R Read moreSource§fn to_box(&self) -> BoxStatefulFunction<T, R>where
T: 'static,
R: 'static,
fn to_box(&self) -> BoxStatefulFunction<T, R>where
T: 'static,
R: 'static,
BoxStatefulFunction. Read moreSource§fn to_rc(&self) -> RcStatefulFunction<T, R>where
T: 'static,
R: 'static,
fn to_rc(&self) -> RcStatefulFunction<T, R>where
T: 'static,
R: 'static,
RcStatefulFunction. Read moreSource§fn to_fn(&self) -> impl FnMut(&T) -> R
fn to_fn(&self) -> impl FnMut(&T) -> R
FnMut(&T) -> R). Read more