pub struct BoxTransformerOnce<T, R> { /* private fields */ }Expand description
BoxTransformerOnce - consuming transformer wrapper based on
Box<dyn FnOnce>
A transformer wrapper that provides single ownership with one-time use semantics. Consumes both self and the input value.
§Features
- Based on:
Box<dyn FnOnce(T) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can only be called once (consumes self and input)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement)
§Author
Haixing Hu
Implementations§
Source§impl<T, R> BoxTransformerOnce<T, R>where
T: 'static,
R: 'static,
impl<T, R> BoxTransformerOnce<T, R>where
T: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnOnce(T) -> R + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnOnce(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: FnOnce(T) -> R + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnOnce(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: FnOnce(T) -> R + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnOnce(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() -> BoxTransformerOnce<T, T>
pub fn identity() -> BoxTransformerOnce<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) -> BoxConditionalTransformerOnce<T, R>where
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<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) -> BoxTransformerOnce<T, S>where
S: 'static,
F: TransformerOnce<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxTransformerOnce<T, S>where
S: 'static,
F: TransformerOnce<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> BoxTransformerOnce<T, R>where
T: 'static,
R: Clone + 'static,
impl<T, R> BoxTransformerOnce<T, R>where
T: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> BoxTransformerOnce<T, R>
pub fn constant(value: R) -> BoxTransformerOnce<T, R>
Creates a constant transformer
§Examples
/// rust /// use prism3_function::{BoxTransformerOnce, Transformer}; /// /// let constant = BoxTransformerOnce::constant("hello"); /// assert_eq!(constant.apply(123), "hello"); ///