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)
Implementations§
Source§impl<T, R> BoxTransformerOnce<T, R>
impl<T, R> BoxTransformerOnce<T, R>
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 clear_name(&mut self)
pub fn clear_name(&mut self)
Clears the name of this transformer.
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
T: 'static,
R: 'static,
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<T, R>where
T: 'static,
R: 'static,
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 qubit_function::transformers::*;
let transformer = BoxTransformer::new({
|value: i32| value * 2
});
let conditional = transformer.when(|value: &i32| *value > 0).or_else(|value: i32| value);
assert_eq!(conditional.apply(5), 10); // transformed
assert_eq!(conditional.apply(-1), -1); // identity (unchanged)Sourcepub fn and_then<S, F>(self, after: F) -> BoxTransformerOnce<T, S>where
T: 'static,
R: 'static,
S: 'static,
F: TransformerOnce<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxTransformerOnce<T, S>where
T: 'static,
R: 'static,
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 qubit_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.apply(5), 12); // (5 + 1) * 2 = 12Examples found in repository?
20fn main() {
21 println!("=== FnTransformerOnceOps Example ===\n");
22
23 // 1. Basic and_then composition
24 println!("1. Basic and_then composition:");
25 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
26 let double = |x: i32| x * 2;
27 let composed = parse.and_then(double);
28 println!(
29 " parse.and_then(double).apply(\"21\") = {}",
30 composed.apply("21".to_string())
31 );
32 println!();
33
34 // 2. Chained and_then composition
35 println!("2. Chained and_then composition:");
36 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
37 let add_one = |x: i32| x + 1;
38 let double = |x: i32| x * 2;
39 let chained = parse.and_then(add_one).and_then(double);
40 println!(
41 " parse.and_then(add_one).and_then(double).apply(\"5\") = {}",
42 chained.apply("5".to_string())
43 ); // (5 + 1) * 2 = 12
44 println!();
45
46 // 3. More and_then composition
47 println!("3. More and_then composition:");
48 let double = |x: i32| x * 2;
49 let to_string = |x: i32| x.to_string();
50 let composed = double.and_then(to_string);
51 println!(" double.and_then(to_string).apply(21) = {}", composed.apply(21)); // (21 * 2).to_string() = "42"
52 println!();
53
54 // 4. Conditional transformation when
55 println!("4. Conditional transformation when:");
56 let double = |x: i32| x * 2;
57 let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
58 println!(" double.when(x > 0).or_else(negate):");
59 println!(" transform(5) = {}", conditional.apply(5)); // 10
60
61 let double2 = |x: i32| x * 2;
62 let conditional2 = double2.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
63 println!(" transform(-5) = {}", conditional2.apply(-5)); // 5
64 println!();
65
66 // 5. Complex composition
67 println!("5. Complex composition:");
68 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
69 let double = |x: i32| x * 2;
70 let triple = |x: i32| x * 3;
71 let to_string = |x: i32| x.to_string();
72
73 let complex = parse
74 .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
75 .and_then(to_string);
76
77 println!(" parse.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
78 println!(" transform(\"3\") = {}", complex.apply("3".to_string())); // 3 <= 5, so 3 * 3 = 9
79
80 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
81 let double2 = |x: i32| x * 2;
82 let triple2 = |x: i32| x * 3;
83 let to_string2 = |x: i32| x.to_string();
84 let complex2 = parse2
85 .and_then(double2.when(|x: &i32| *x > 5).or_else(triple2))
86 .and_then(to_string2);
87 println!(" transform(\"10\") = {}", complex2.apply("10".to_string())); // 10 > 5, so 10 * 2 = 20
88 println!();
89
90 // 6. Type conversion
91 println!("6. Type conversion:");
92 let to_string = |x: i32| x.to_string();
93 let get_length = |s: String| s.len();
94 let length_transformer = to_string.and_then(get_length);
95 println!(
96 " to_string.and_then(get_length).apply(12345) = {}",
97 length_transformer.apply(12345)
98 ); // 5
99 println!();
100
101 // 7. Closures that capture environment
102 println!("7. Closures that capture environment:");
103 let multiplier = 3;
104 let multiply = move |x: i32| x * multiplier;
105 let add_ten = |x: i32| x + 10;
106 let with_capture = multiply.and_then(add_ten);
107 println!(" multiply(3).and_then(add_ten).apply(5) = {}", with_capture.apply(5)); // 5 * 3 + 10 = 25
108 println!();
109
110 // 8. Function pointers
111 println!("8. Function pointers:");
112 fn parse_fn(s: String) -> i32 {
113 s.parse().unwrap_or(0)
114 }
115 fn double_fn(x: i32) -> i32 {
116 x * 2
117 }
118 let fn_composed = parse_fn.and_then(double_fn);
119 println!(
120 " parse_fn.and_then(double_fn).apply(\"21\") = {}",
121 fn_composed.apply("21".to_string())
122 ); // 42
123 println!();
124
125 // 9. String operations that consume ownership
126 println!("9. String operations that consume ownership:");
127 let owned = String::from("hello");
128 let append = move |s: String| format!("{} {}", s, owned);
129 let uppercase = |s: String| s.to_uppercase();
130 let composed = append.and_then(uppercase);
131 println!(
132 " append.and_then(uppercase).apply(\"world\") = {}",
133 composed.apply("world".to_string())
134 ); // "WORLD HELLO"
135 println!();
136
137 // 10. Parsing and validation
138 println!("10. Parsing and validation:");
139 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
140 let validate = |x: i32| if x > 0 { x } else { 1 };
141 let composed = parse.and_then(validate);
142 println!(
143 " parse.and_then(validate).apply(\"42\") = {}",
144 composed.apply("42".to_string())
145 ); // 42
146
147 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
148 let validate2 = |x: i32| if x > 0 { x } else { 1 };
149 let composed2 = parse2.and_then(validate2);
150 println!(
151 " parse.and_then(validate).apply(\"-5\") = {}",
152 composed2.apply("-5".to_string())
153 ); // 1
154 println!();
155
156 println!("=== Example completed ===");
157}Source§impl<T, R> BoxTransformerOnce<T, R>
impl<T, R> BoxTransformerOnce<T, R>
Sourcepub fn constant(value: R) -> BoxTransformerOnce<T, R>where
R: Clone + 'static,
pub fn constant(value: R) -> BoxTransformerOnce<T, R>where
R: Clone + 'static,
Creates a constant transformer
§Examples
/// rust /// use qubit_function::{BoxTransformerOnce, Transformer}; /// /// let constant = BoxTransformerOnce::constant("hello"); /// assert_eq!(constant.apply(123), "hello"); ///