pub struct BoxStatefulTransformer<T, R> { /* private fields */ }Expand description
BoxStatefulTransformer - transformer wrapper based on Box<dyn FnMut>
A transformer wrapper that provides single ownership with reusable stateful transformation. The transformer consumes the input and can be called multiple times while maintaining internal state.
§Features
- Based on:
Box<dyn FnMut(T) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can be called multiple times (each call consumes its input)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement) - Statefulness: Can modify internal state between calls
§Author
Haixing Hu
Implementations§
Source§impl<T, R> BoxStatefulTransformer<T, R>where
T: 'static,
R: 'static,
impl<T, R> BoxStatefulTransformer<T, R>where
T: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnMut(T) -> R + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnMut(T) -> R + 'static,
Creates a new transformer.
Wraps the provided closure in the appropriate smart pointer type for this transformer implementation.
Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut(T) -> R + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut(T) -> R + 'static,
Creates a new named transformer.
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>) -> Selfwhere
F: FnMut(T) -> R + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnMut(T) -> R + 'static,
Creates a new named transformer with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn identity() -> BoxStatefulTransformer<T, T>
pub fn identity() -> BoxStatefulTransformer<T, T>
Creates an identity transformer.
Creates a transformer that returns the input value unchanged. Useful for default values or placeholder implementations.
§Returns
Returns a new transformer instance that returns the input unchanged.
Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalStatefulTransformer<T, R>where
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalStatefulTransformer<T, R>where
P: Predicate<T> + 'static,
Creates a conditional transformer that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the transformation operation
§Returns
Returns a conditional transformer that only executes when the
predicate returns true.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use prism3_rust_function::transformers::*;
let transformer = BoxTransformer::new({
|value: &i32| value * 2
});
let conditional = transformer.when(|value: &i32| *value > 0);
assert_eq!(conditional.transform(&5), 10); // transformed
assert_eq!(conditional.transform(&-1), -1); // identity (unchanged)Sourcepub fn and_then<S, F>(self, after: F) -> BoxStatefulTransformer<T, S>where
S: 'static,
F: StatefulTransformer<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxStatefulTransformer<T, S>where
S: 'static,
F: StatefulTransformer<R, S> + 'static,
Chains execution with another transformer, executing the current transformer first, then the subsequent transformer.
§Parameters
after- The subsequent transformer to execute after the current transformer completes
§Returns
Returns a new transformer that executes the current transformer and the subsequent transformer in sequence.
§Examples
use prism3_rust_function::transformers::*;
let transformer1 = BoxTransformer::new({
|value: &i32| value + 1
});
let transformer2 = BoxTransformer::new({
|value: &i32| value * 2
});
let chained = transformer1.and_then(transformer2);
assert_eq!(chained.transform(&5), 12); // (5 + 1) * 2 = 12Source§impl<T, R> BoxStatefulTransformer<T, R>where
T: 'static,
R: Clone + 'static,
impl<T, R> BoxStatefulTransformer<T, R>where
T: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> BoxStatefulTransformer<T, R>
pub fn constant(value: R) -> BoxStatefulTransformer<T, R>
Creates a constant transformer
§Examples
/// rust /// use prism3_function::{BoxStatefulTransformer, StatefulTransformer}; /// /// let mut constant = BoxStatefulTransformer::constant("hello"); /// assert_eq!(constant.apply(123), "hello"); ///
Trait Implementations§
Source§impl<T, R> Debug for BoxStatefulTransformer<T, R>
impl<T, R> Debug for BoxStatefulTransformer<T, R>
Source§impl<T, R> Display for BoxStatefulTransformer<T, R>
impl<T, R> Display for BoxStatefulTransformer<T, R>
Source§impl<T, R> StatefulTransformer<T, R> for BoxStatefulTransformer<T, R>
impl<T, R> StatefulTransformer<T, R> for BoxStatefulTransformer<T, R>
Source§fn apply(&mut self, input: T) -> R
fn apply(&mut self, input: T) -> R
Source§fn into_box(self) -> BoxStatefulTransformer<T, R>where
T: 'static,
R: 'static,
fn into_box(self) -> BoxStatefulTransformer<T, R>where
T: 'static,
R: 'static,
Source§fn into_rc(self) -> RcStatefulTransformer<T, R>where
T: 'static,
R: 'static,
fn into_rc(self) -> RcStatefulTransformer<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 into_once(self) -> BoxTransformerOnce<T, R>where
T: 'static,
R: 'static,
fn into_once(self) -> BoxTransformerOnce<T, R>where
T: 'static,
R: 'static,
BoxTransformerOnce. Read more