pub trait StatefulTransformer<T, R> {
Show 13 methods
// Required method
fn apply(&mut self, input: T) -> R;
// Provided methods
fn into_box(self) -> BoxStatefulTransformer<T, R>
where Self: Sized + 'static { ... }
fn into_rc(self) -> RcStatefulTransformer<T, R>
where Self: Sized + 'static { ... }
fn into_arc(self) -> ArcStatefulTransformer<T, R>
where Self: Sized + Send + 'static { ... }
fn into_fn(self) -> impl FnMut(T) -> R
where Self: Sized + 'static { ... }
fn into_mut_fn(self) -> impl FnMut(T) -> R
where Self: Sized + 'static { ... }
fn into_once(self) -> BoxTransformerOnce<T, R>
where Self: Sized + 'static { ... }
fn to_box(&self) -> BoxStatefulTransformer<T, R>
where Self: Sized + Clone + 'static { ... }
fn to_rc(&self) -> RcStatefulTransformer<T, R>
where Self: Sized + Clone + 'static { ... }
fn to_arc(&self) -> ArcStatefulTransformer<T, R>
where Self: Sized + Clone + Send + 'static { ... }
fn to_fn(&self) -> impl FnMut(T) -> R
where Self: Sized + Clone + 'static { ... }
fn to_mut_fn(&self) -> impl FnMut(T) -> R
where Self: Sized + Clone + 'static { ... }
fn to_once(&self) -> BoxTransformerOnce<T, R>
where Self: Sized + Clone + 'static { ... }
}Expand description
StatefulTransformer trait - transforms values from type T to type R with state
Defines the behavior of a stateful transformation: converting a value
of type T to a value of type R by consuming the input while
allowing modification of internal state. This is analogous to
FnMut(T) -> R in Rust’s standard library.
§Type Parameters
T- The type of the input value (consumed)R- The type of the output value
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxStatefulTransformer<T, R>where
Self: Sized + 'static,
fn into_box(self) -> BoxStatefulTransformer<T, R>where
Self: Sized + 'static,
Converts to BoxStatefulTransformer
⚠️ Consumes self: The original transformer becomes unavailable
after calling this method.
§Returns
Returns BoxStatefulTransformer<T, R>
§Default Implementation
The default implementation wraps self in a BoxStatefulTransformer by
creating a new closure that calls self.apply(). This is a lightweight
adapter, but it is not strictly zero-cost.
§Examples
use qubit_function::{StatefulTransformer, BoxStatefulTransformer};
struct CustomTransformer {
multiplier: i32,
}
impl StatefulTransformer<i32, i32> for CustomTransformer {
fn apply(&mut self, input: i32) -> i32 {
self.multiplier += 1;
input * self.multiplier
}
}
let transformer = CustomTransformer { multiplier: 0 };
let mut boxed = transformer.into_box();
assert_eq!(boxed.apply(10), 10); // 10 * 1
assert_eq!(boxed.apply(10), 20); // 10 * 2Examples found in repository?
24fn main() {
25 println!("=== StatefulTransformer Demo ===\n");
26
27 // 1. Basic BoxStatefulTransformer with state
28 println!("1. BoxStatefulTransformer with stateful counter:");
29 let mut counter = 0;
30 let mut mapper = BoxStatefulTransformer::new(move |x: i32| {
31 counter += 1;
32 format!("Item #{}: {}", counter, x)
33 });
34
35 println!(" {}", mapper.apply(100)); // Item #1: 100
36 println!(" {}", mapper.apply(200)); // Item #2: 200
37 println!(" {}", mapper.apply(300)); // Item #3: 300
38
39 // 2. Composing mappers with and_then
40 println!("\n2. Composing mappers with and_then:");
41 let mut counter1 = 0;
42 let mapper1 = BoxStatefulTransformer::new(move |x: i32| {
43 counter1 += 1;
44 x + counter1
45 });
46
47 let mut counter2 = 0;
48 let mapper2 = BoxStatefulTransformer::new(move |x: i32| {
49 counter2 += 1;
50 x * counter2
51 });
52
53 let mut composed = mapper1.and_then(mapper2);
54 println!(" First call: {}", composed.apply(10)); // (10 + 1) * 1 = 11
55 println!(" Second call: {}", composed.apply(10)); // (10 + 2) * 2 = 24
56 println!(" Third call: {}", composed.apply(10)); // (10 + 3) * 3 = 39
57
58 // 3. Conditional mapping with when/or_else
59 println!("\n3. Conditional mapping:");
60 let mut high_count = 0;
61 let mut low_count = 0;
62
63 let mut conditional = BoxStatefulTransformer::new(move |x: i32| {
64 high_count += 1;
65 format!("High[{}]: {} * 2 = {}", high_count, x, x * 2)
66 })
67 .when(|x: &i32| *x >= 10)
68 .or_else(move |x| {
69 low_count += 1;
70 format!("Low[{}]: {} + 1 = {}", low_count, x, x + 1)
71 });
72
73 println!(" {}", conditional.apply(15)); // High[1]: 15 * 2 = 30
74 println!(" {}", conditional.apply(5)); // Low[1]: 5 + 1 = 6
75 println!(" {}", conditional.apply(20)); // High[2]: 20 * 2 = 40
76
77 // 4. RcStatefulTransformer for cloneable mappers
78 println!("\n4. RcStatefulTransformer (cloneable, single-threaded):");
79 let mut counter = 0;
80 let mapper = RcStatefulTransformer::new(move |x: i32| {
81 counter += 1;
82 x + counter
83 });
84
85 let mut mapper1 = mapper.clone();
86 let mut mapper2 = mapper.clone();
87
88 println!(" mapper1: {}", mapper1.apply(10)); // 11
89 println!(" mapper2: {}", mapper2.apply(10)); // 12
90 println!(" mapper1: {}", mapper1.apply(10)); // 13
91
92 // 5. ArcStatefulTransformer for thread-safe mappers
93 println!("\n5. ArcStatefulTransformer (thread-safe):");
94 let mut counter = 0;
95 let mapper = ArcStatefulTransformer::new(move |x: i32| {
96 counter += 1;
97 format!("Result[{}]: {}", counter, x * 2)
98 });
99
100 let mut mapper_clone = mapper.clone();
101 println!(" Original: {}", mapper_clone.apply(5)); // Result[1]: 10
102 println!(" Clone: {}", mapper_clone.apply(7)); // Result[2]: 14
103
104 // 6. Using FnStatefulTransformerOps extension trait
105 println!("\n6. Using FnStatefulTransformerOps extension trait:");
106 let mut count = 0;
107 let mut mapper = (move |x: i32| {
108 count += 1;
109 x + count
110 })
111 .and_then(|x| x * 2);
112
113 println!(" {}", mapper.apply(10)); // (10 + 1) * 2 = 22
114 println!(" {}", mapper.apply(10)); // (10 + 2) * 2 = 24
115
116 // 7. Building a complex pipeline
117 println!("\n7. Complex processing pipeline:");
118 let mut step1_count = 0;
119 let step1 = BoxStatefulTransformer::new(move |x: i32| {
120 step1_count += 1;
121 format!("Step1[{}]: {}", step1_count, x)
122 });
123
124 let mut step2_count = 0;
125 let step2 = BoxStatefulTransformer::new(move |s: String| {
126 step2_count += 1;
127 format!("{} -> Step2[{}]", s, step2_count)
128 });
129
130 let mut step3_count = 0;
131 let step3 = BoxStatefulTransformer::new(move |s: String| {
132 step3_count += 1;
133 format!("{} -> Step3[{}]", s, step3_count)
134 });
135
136 let mut pipeline = step1.and_then(step2).and_then(step3);
137
138 println!(" {}", pipeline.apply(100));
139 println!(" {}", pipeline.apply(200));
140
141 // 7. TransformerOnce implementation - consuming transformers
142 println!("\n7. TransformerOnce implementation - consuming StatefulTransformers:");
143
144 // BoxStatefulTransformer can be consumed as TransformerOnce
145 let mut counter = 0;
146 let mut box_mapper = BoxStatefulTransformer::new(move |x: i32| {
147 counter += 1;
148 x * counter
149 });
150 println!(
151 " BoxStatefulTransformer consumed once: {}",
152 box_mapper.apply(10)
153 ); // 10 * 1 = 10
154
155 // RcStatefulTransformer can be consumed as TransformerOnce
156 let mut counter = 0;
157 let mut rc_mapper = RcStatefulTransformer::new(move |x: i32| {
158 counter += 1;
159 x + counter
160 });
161 let rc_clone = rc_mapper.clone(); // Clone before consuming
162 println!(
163 " RcStatefulTransformer consumed once: {}",
164 rc_mapper.apply(10)
165 ); // 10 + 1 = 11
166 println!(" RcStatefulTransformer clone still works: {}", {
167 let mut rc_clone_for_call = rc_clone.clone();
168 rc_clone_for_call.apply(10)
169 }); // 10 + 2 = 12
170
171 // ArcStatefulTransformer can be consumed as TransformerOnce
172 let mut counter = 0;
173 let mut arc_mapper = ArcStatefulTransformer::new(move |x: i32| {
174 counter += 1;
175 x * counter
176 });
177 let arc_clone = arc_mapper.clone(); // Clone before consuming
178 println!(
179 " ArcStatefulTransformer consumed once: {}",
180 arc_mapper.apply(10)
181 ); // 10 * 1 = 10
182 println!(" ArcStatefulTransformer clone still works: {}", {
183 let mut arc_clone_for_call = arc_clone.clone();
184 arc_clone_for_call.apply(10)
185 }); // 10 * 2 = 20
186
187 // 8. Converting to BoxTransformerOnce
188 println!("\n8. Converting StatefulTransformers to BoxTransformerOnce:");
189
190 let mut counter = 0;
191 let mapper = BoxStatefulTransformer::new(move |x: i32| {
192 counter += 1;
193 x * counter
194 });
195 let mut once_mapper = mapper.into_box();
196 println!(
197 " BoxStatefulTransformer->BoxTransformerOnce: {}",
198 once_mapper.apply(5)
199 ); // 5 * 1 = 5
200
201 // RcStatefulTransformer can use to_box() to preserve original
202 let mut counter = 0;
203 let rc_mapper = RcStatefulTransformer::new(move |x: i32| {
204 counter += 1;
205 x * counter
206 });
207 let mut once_mapper = rc_mapper.to_box();
208 println!(
209 " RcStatefulTransformer->BoxTransformerOnce: {}",
210 once_mapper.apply(5)
211 ); // 5 * 1 = 5
212 println!(" Original RcStatefulTransformer still works: {}", {
213 let mut rc_original_for_call = rc_mapper.clone();
214 rc_original_for_call.apply(5)
215 }); // 5 * 2 = 10
216
217 println!("\n=== Demo Complete ===");
218}Sourcefn into_rc(self) -> RcStatefulTransformer<T, R>where
Self: Sized + 'static,
fn into_rc(self) -> RcStatefulTransformer<T, R>where
Self: Sized + 'static,
Converts to RcStatefulTransformer
⚠️ Consumes self: The original transformer becomes unavailable
after calling this method.
§Returns
Returns RcStatefulTransformer<T, R>
§Default Implementation
The default implementation first converts to BoxStatefulTransformer using
into_box(), then wraps it in RcStatefulTransformer. Specific implementations
may override this for better efficiency.
§Examples
use qubit_function::{StatefulTransformer, RcStatefulTransformer};
struct CustomTransformer {
multiplier: i32,
}
impl StatefulTransformer<i32, i32> for CustomTransformer {
fn apply(&mut self, input: i32) -> i32 {
self.multiplier += 1;
input * self.multiplier
}
}
let transformer = CustomTransformer { multiplier: 0 };
let mut rc_transformer = transformer.into_rc();
assert_eq!(rc_transformer.apply(10), 10); // 10 * 1
assert_eq!(rc_transformer.apply(10), 20); // 10 * 2Sourcefn into_arc(self) -> ArcStatefulTransformer<T, R>
fn into_arc(self) -> ArcStatefulTransformer<T, R>
Converts to ArcStatefulTransformer
⚠️ Consumes self: The original transformer becomes unavailable
after calling this method.
§Returns
Returns ArcStatefulTransformer<T, R>
§Default Implementation
The default implementation wraps self in an ArcStatefulTransformer by creating
a new closure that calls self.apply(). Note that this requires self
to implement Send due to Arc’s thread-safety requirements.
§Examples
use qubit_function::{StatefulTransformer, ArcStatefulTransformer};
struct CustomTransformer {
multiplier: i32,
}
impl StatefulTransformer<i32, i32> for CustomTransformer {
fn apply(&mut self, input: i32) -> i32 {
self.multiplier += 1;
input * self.multiplier
}
}
let transformer = CustomTransformer { multiplier: 0 };
let mut arc_transformer = transformer.into_arc();
assert_eq!(arc_transformer.apply(10), 10); // 10 * 1
assert_eq!(arc_transformer.apply(10), 20); // 10 * 2Sourcefn into_fn(self) -> impl FnMut(T) -> Rwhere
Self: Sized + 'static,
fn into_fn(self) -> impl FnMut(T) -> Rwhere
Self: Sized + 'static,
Converts to a closure implementing FnMut(T) -> R
⚠️ Consumes self: The original transformer becomes unavailable
after calling this method.
§Returns
Returns an implementation of FnMut(T) -> R
§Default Implementation
The default implementation creates a new closure that calls self.apply().
Specific implementations may override this for better efficiency.
§Examples
use qubit_function::{StatefulTransformer, BoxStatefulTransformer};
let transformer = BoxStatefulTransformer::new(|x: i32| x * 2);
let mut closure = transformer.into_fn();
assert_eq!(closure(10), 20);
assert_eq!(closure(15), 30);Sourcefn into_mut_fn(self) -> impl FnMut(T) -> Rwhere
Self: Sized + 'static,
fn into_mut_fn(self) -> impl FnMut(T) -> Rwhere
Self: Sized + 'static,
Converts transformer to a mutable closure (FnMut) with an explicit
method name.
This is a naming alias of StatefulTransformer::into_fn to make the
mutability of the returned closure explicit.
Sourcefn into_once(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
fn into_once(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
Converts to BoxTransformerOnce.
This method has a default implementation that wraps the
transformer in a BoxTransformerOnce. Custom implementations
can override this method for optimization purposes.
§Returns
A new BoxTransformerOnce<T, R> instance
§Examples
use qubit_function::{StatefulTransformer, TransformerOnce};
let closure = |x: i32| x * 2;
let once = closure.into_once();
assert_eq!(once.apply(5), 10);Sourcefn to_box(&self) -> BoxStatefulTransformer<T, R>
fn to_box(&self) -> BoxStatefulTransformer<T, R>
Non-consuming conversion to BoxStatefulTransformer.
Default implementation requires Self: Clone and wraps a cloned
instance in a RefCell so the returned transformer can mutate state
across calls.
Examples found in repository?
24fn main() {
25 println!("=== StatefulTransformer Demo ===\n");
26
27 // 1. Basic BoxStatefulTransformer with state
28 println!("1. BoxStatefulTransformer with stateful counter:");
29 let mut counter = 0;
30 let mut mapper = BoxStatefulTransformer::new(move |x: i32| {
31 counter += 1;
32 format!("Item #{}: {}", counter, x)
33 });
34
35 println!(" {}", mapper.apply(100)); // Item #1: 100
36 println!(" {}", mapper.apply(200)); // Item #2: 200
37 println!(" {}", mapper.apply(300)); // Item #3: 300
38
39 // 2. Composing mappers with and_then
40 println!("\n2. Composing mappers with and_then:");
41 let mut counter1 = 0;
42 let mapper1 = BoxStatefulTransformer::new(move |x: i32| {
43 counter1 += 1;
44 x + counter1
45 });
46
47 let mut counter2 = 0;
48 let mapper2 = BoxStatefulTransformer::new(move |x: i32| {
49 counter2 += 1;
50 x * counter2
51 });
52
53 let mut composed = mapper1.and_then(mapper2);
54 println!(" First call: {}", composed.apply(10)); // (10 + 1) * 1 = 11
55 println!(" Second call: {}", composed.apply(10)); // (10 + 2) * 2 = 24
56 println!(" Third call: {}", composed.apply(10)); // (10 + 3) * 3 = 39
57
58 // 3. Conditional mapping with when/or_else
59 println!("\n3. Conditional mapping:");
60 let mut high_count = 0;
61 let mut low_count = 0;
62
63 let mut conditional = BoxStatefulTransformer::new(move |x: i32| {
64 high_count += 1;
65 format!("High[{}]: {} * 2 = {}", high_count, x, x * 2)
66 })
67 .when(|x: &i32| *x >= 10)
68 .or_else(move |x| {
69 low_count += 1;
70 format!("Low[{}]: {} + 1 = {}", low_count, x, x + 1)
71 });
72
73 println!(" {}", conditional.apply(15)); // High[1]: 15 * 2 = 30
74 println!(" {}", conditional.apply(5)); // Low[1]: 5 + 1 = 6
75 println!(" {}", conditional.apply(20)); // High[2]: 20 * 2 = 40
76
77 // 4. RcStatefulTransformer for cloneable mappers
78 println!("\n4. RcStatefulTransformer (cloneable, single-threaded):");
79 let mut counter = 0;
80 let mapper = RcStatefulTransformer::new(move |x: i32| {
81 counter += 1;
82 x + counter
83 });
84
85 let mut mapper1 = mapper.clone();
86 let mut mapper2 = mapper.clone();
87
88 println!(" mapper1: {}", mapper1.apply(10)); // 11
89 println!(" mapper2: {}", mapper2.apply(10)); // 12
90 println!(" mapper1: {}", mapper1.apply(10)); // 13
91
92 // 5. ArcStatefulTransformer for thread-safe mappers
93 println!("\n5. ArcStatefulTransformer (thread-safe):");
94 let mut counter = 0;
95 let mapper = ArcStatefulTransformer::new(move |x: i32| {
96 counter += 1;
97 format!("Result[{}]: {}", counter, x * 2)
98 });
99
100 let mut mapper_clone = mapper.clone();
101 println!(" Original: {}", mapper_clone.apply(5)); // Result[1]: 10
102 println!(" Clone: {}", mapper_clone.apply(7)); // Result[2]: 14
103
104 // 6. Using FnStatefulTransformerOps extension trait
105 println!("\n6. Using FnStatefulTransformerOps extension trait:");
106 let mut count = 0;
107 let mut mapper = (move |x: i32| {
108 count += 1;
109 x + count
110 })
111 .and_then(|x| x * 2);
112
113 println!(" {}", mapper.apply(10)); // (10 + 1) * 2 = 22
114 println!(" {}", mapper.apply(10)); // (10 + 2) * 2 = 24
115
116 // 7. Building a complex pipeline
117 println!("\n7. Complex processing pipeline:");
118 let mut step1_count = 0;
119 let step1 = BoxStatefulTransformer::new(move |x: i32| {
120 step1_count += 1;
121 format!("Step1[{}]: {}", step1_count, x)
122 });
123
124 let mut step2_count = 0;
125 let step2 = BoxStatefulTransformer::new(move |s: String| {
126 step2_count += 1;
127 format!("{} -> Step2[{}]", s, step2_count)
128 });
129
130 let mut step3_count = 0;
131 let step3 = BoxStatefulTransformer::new(move |s: String| {
132 step3_count += 1;
133 format!("{} -> Step3[{}]", s, step3_count)
134 });
135
136 let mut pipeline = step1.and_then(step2).and_then(step3);
137
138 println!(" {}", pipeline.apply(100));
139 println!(" {}", pipeline.apply(200));
140
141 // 7. TransformerOnce implementation - consuming transformers
142 println!("\n7. TransformerOnce implementation - consuming StatefulTransformers:");
143
144 // BoxStatefulTransformer can be consumed as TransformerOnce
145 let mut counter = 0;
146 let mut box_mapper = BoxStatefulTransformer::new(move |x: i32| {
147 counter += 1;
148 x * counter
149 });
150 println!(
151 " BoxStatefulTransformer consumed once: {}",
152 box_mapper.apply(10)
153 ); // 10 * 1 = 10
154
155 // RcStatefulTransformer can be consumed as TransformerOnce
156 let mut counter = 0;
157 let mut rc_mapper = RcStatefulTransformer::new(move |x: i32| {
158 counter += 1;
159 x + counter
160 });
161 let rc_clone = rc_mapper.clone(); // Clone before consuming
162 println!(
163 " RcStatefulTransformer consumed once: {}",
164 rc_mapper.apply(10)
165 ); // 10 + 1 = 11
166 println!(" RcStatefulTransformer clone still works: {}", {
167 let mut rc_clone_for_call = rc_clone.clone();
168 rc_clone_for_call.apply(10)
169 }); // 10 + 2 = 12
170
171 // ArcStatefulTransformer can be consumed as TransformerOnce
172 let mut counter = 0;
173 let mut arc_mapper = ArcStatefulTransformer::new(move |x: i32| {
174 counter += 1;
175 x * counter
176 });
177 let arc_clone = arc_mapper.clone(); // Clone before consuming
178 println!(
179 " ArcStatefulTransformer consumed once: {}",
180 arc_mapper.apply(10)
181 ); // 10 * 1 = 10
182 println!(" ArcStatefulTransformer clone still works: {}", {
183 let mut arc_clone_for_call = arc_clone.clone();
184 arc_clone_for_call.apply(10)
185 }); // 10 * 2 = 20
186
187 // 8. Converting to BoxTransformerOnce
188 println!("\n8. Converting StatefulTransformers to BoxTransformerOnce:");
189
190 let mut counter = 0;
191 let mapper = BoxStatefulTransformer::new(move |x: i32| {
192 counter += 1;
193 x * counter
194 });
195 let mut once_mapper = mapper.into_box();
196 println!(
197 " BoxStatefulTransformer->BoxTransformerOnce: {}",
198 once_mapper.apply(5)
199 ); // 5 * 1 = 5
200
201 // RcStatefulTransformer can use to_box() to preserve original
202 let mut counter = 0;
203 let rc_mapper = RcStatefulTransformer::new(move |x: i32| {
204 counter += 1;
205 x * counter
206 });
207 let mut once_mapper = rc_mapper.to_box();
208 println!(
209 " RcStatefulTransformer->BoxTransformerOnce: {}",
210 once_mapper.apply(5)
211 ); // 5 * 1 = 5
212 println!(" Original RcStatefulTransformer still works: {}", {
213 let mut rc_original_for_call = rc_mapper.clone();
214 rc_original_for_call.apply(5)
215 }); // 5 * 2 = 10
216
217 println!("\n=== Demo Complete ===");
218}Sourcefn to_rc(&self) -> RcStatefulTransformer<T, R>
fn to_rc(&self) -> RcStatefulTransformer<T, R>
Non-consuming conversion to RcStatefulTransformer.
Default implementation clones self into an Rc<RefCell<_>> so the
resulting transformer can be shared within a single thread.
Sourcefn to_arc(&self) -> ArcStatefulTransformer<T, R>
fn to_arc(&self) -> ArcStatefulTransformer<T, R>
Non-consuming conversion to ArcStatefulTransformer (thread-safe).
Default implementation requires Self: Clone + Send + Sync and wraps
the cloned instance in Arc<Mutex<_>> so it can be used across
threads.
Sourcefn to_fn(&self) -> impl FnMut(T) -> R
fn to_fn(&self) -> impl FnMut(T) -> R
Non-consuming conversion to a closure (FnMut(T) -> R).
Default implementation clones self into a RefCell and returns a
closure that calls apply on the interior mutable value.
Sourcefn to_mut_fn(&self) -> impl FnMut(T) -> R
fn to_mut_fn(&self) -> impl FnMut(T) -> R
Non-consuming conversion to a mutable closure (FnMut) with an explicit
method name.
This is a naming alias of StatefulTransformer::to_fn and preserves
the same clone-based behavior.
Sourcefn to_once(&self) -> BoxTransformerOnce<T, R>
fn to_once(&self) -> BoxTransformerOnce<T, R>
Creates a BoxTransformerOnce from a cloned transformer
Uses Clone to obtain an owned copy and converts it into a
BoxTransformerOnce. Requires Self: Clone. Custom implementations
can override this for better performance.