pub struct BoxConditionalStatefulTransformer<T, R> { /* private fields */ }Expand description
BoxConditionalStatefulTransformer struct
A conditional transformer that only executes when a predicate is satisfied.
Uses BoxStatefulTransformer and BoxPredicate for single ownership semantics.
This type is typically created by calling BoxStatefulTransformer::when() and is
designed to work with the or_else() method to create if-then-else
logic.
§Features
- Single Ownership: Not cloneable, consumes
selfon use - Conditional Execution: Only maps when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic - Implements StatefulTransformer: Can be used anywhere a
StatefulTransformeris expected
§Examples
use prism3_function::{StatefulTransformer, BoxStatefulTransformer};
let mut high_count = 0;
let mut low_count = 0;
let mut transformer = BoxStatefulTransformer::new(move |x: i32| {
high_count += 1;
x * 2
})
.when(|x: &i32| *x >= 10)
.or_else(move |x| {
low_count += 1;
x + 1
});
assert_eq!(transformer.apply(15), 30); // when branch executed
assert_eq!(transformer.apply(5), 6); // or_else branch executed§Author
Haixing Hu
Implementations§
Source§impl<T, R> BoxConditionalStatefulTransformer<T, R>where
T: 'static,
R: 'static,
impl<T, R> BoxConditionalStatefulTransformer<T, R>where
T: 'static,
R: 'static,
Sourcepub fn or_else<F>(self, else_transformer: F) -> BoxStatefulTransformer<T, R>where
F: StatefulTransformer<T, R> + 'static,
pub fn or_else<F>(self, else_transformer: F) -> BoxStatefulTransformer<T, R>where
F: StatefulTransformer<T, R> + 'static,
Trait Implementations§
Source§impl<T, R> Debug for BoxConditionalStatefulTransformer<T, R>
impl<T, R> Debug for BoxConditionalStatefulTransformer<T, R>
Auto Trait Implementations§
impl<T, R> Freeze for BoxConditionalStatefulTransformer<T, R>
impl<T, R> !RefUnwindSafe for BoxConditionalStatefulTransformer<T, R>
impl<T, R> !Send for BoxConditionalStatefulTransformer<T, R>
impl<T, R> !Sync for BoxConditionalStatefulTransformer<T, R>
impl<T, R> Unpin for BoxConditionalStatefulTransformer<T, R>
impl<T, R> !UnwindSafe for BoxConditionalStatefulTransformer<T, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more