pub trait FnMutatorOnceOps<T>: FnOnce(&mut T) + Sized {
// Provided method
fn and_then<C>(self, next: C) -> BoxMutatorOnce<T>
where Self: 'static,
C: MutatorOnce<T> + 'static,
T: 'static { ... }
}Expand description
Extension trait providing one-time mutator composition methods for closures
Provides and_then and other composition methods for all closures that
implement FnOnce(&mut T), enabling direct method chaining on closures
without explicit wrapper types.
§Features
- Natural Syntax: Chain operations directly on closures
- Returns BoxMutatorOnce: Composition results are
BoxMutatorOnce<T>for continued chaining - Zero Cost: No overhead when composing closures
- Automatic Implementation: All
FnOnce(&mut T)closures get these methods automatically
§Examples
use qubit_function::{MutatorOnce, FnMutatorOnceOps};
let data1 = vec![1, 2];
let data2 = vec![3, 4];
let chained = (move |x: &mut Vec<i32>| x.extend(data1))
.and_then(move |x: &mut Vec<i32>| x.extend(data2));
let mut target = vec![0];
chained.apply(&mut target);
assert_eq!(target, vec![0, 1, 2, 3, 4]);Provided Methods§
Sourcefn and_then<C>(self, next: C) -> BoxMutatorOnce<T>where
Self: 'static,
C: MutatorOnce<T> + 'static,
T: 'static,
fn and_then<C>(self, next: C) -> BoxMutatorOnce<T>where
Self: 'static,
C: MutatorOnce<T> + 'static,
T: 'static,
Chains another mutator in sequence
Returns a new mutator that first executes the current operation, then
executes the next operation. Consumes the current closure and returns
BoxMutatorOnce<T>.
§Parameters
next- The mutator to execute after the current operation. Note: This parameter is passed by value and will transfer ownership. SinceBoxMutatorOncecannot be cloned, the parameter will be consumed. Can be:- A closure:
|x: &mut T| - A
BoxMutatorOnce<T> - Any type implementing
MutatorOnce<T>
- A closure:
§Returns
Returns the composed BoxMutatorOnce<T>
§Examples
use qubit_function::{MutatorOnce, FnMutatorOnceOps};
let data1 = vec![1, 2];
let data2 = vec![3, 4];
// Both closures are moved and consumed
let chained = (move |x: &mut Vec<i32>| x.extend(data1))
.and_then(move |x: &mut Vec<i32>| x.extend(data2));
let mut target = vec![0];
chained.apply(&mut target);
assert_eq!(target, vec![0, 1, 2, 3, 4]);
// The original closures are consumed and no longer usableExamples found in repository?
examples/mutators/mutator_once_demo.rs (lines 134-137)
20fn main() {
21 println!("=== MutatorOnce Examples ===\n");
22
23 // 1. Basic usage: moving captured variables
24 println!("1. Basic usage: moving captured variables");
25 let data = vec![1, 2, 3];
26 let mutator = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
27 println!(" Adding data: {:?}", data);
28 x.extend(data);
29 });
30
31 let mut target = vec![0];
32 mutator.apply(&mut target);
33 println!(" Result: {:?}\n", target);
34
35 // 2. Method chaining: combining multiple operations
36 println!("2. Method chaining: combining multiple operations");
37 let prefix = vec![1, 2];
38 let middle = vec![3, 4];
39 let suffix = vec![5, 6];
40
41 let chained = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
42 println!(" Adding prefix: {:?}", prefix);
43 x.extend(prefix);
44 })
45 .and_then(move |x: &mut Vec<i32>| {
46 println!(" Adding middle: {:?}", middle);
47 x.extend(middle);
48 })
49 .and_then(move |x: &mut Vec<i32>| {
50 println!(" Adding suffix: {:?}", suffix);
51 x.extend(suffix);
52 });
53
54 let mut result = vec![0];
55 chained.apply(&mut result);
56 println!(" Result: {:?}\n", result);
57
58 // 3. Initializer pattern
59 println!("3. Initializer pattern");
60
61 struct Initializer {
62 name: String,
63 on_complete: Option<BoxMutatorOnce<Vec<String>>>,
64 }
65
66 impl Initializer {
67 fn new<F>(name: impl Into<String>, callback: F) -> Self
68 where
69 F: FnOnce(&mut Vec<String>) + 'static,
70 {
71 Self {
72 name: name.into(),
73 on_complete: Some(BoxMutatorOnce::new(callback)),
74 }
75 }
76
77 fn run(mut self, data: &mut Vec<String>) {
78 println!(" Initializer '{}' is running", self.name);
79 data.push(format!("Initialized by {}", self.name));
80
81 if let Some(callback) = self.on_complete.take() {
82 println!(" Executing completion callback");
83 callback.apply(data);
84 }
85 }
86 }
87
88 let extra = vec!["extra1".to_string(), "extra2".to_string()];
89 let init = Initializer::new("MainInit", move |values| {
90 println!(" Adding extra data in callback: {:?}", extra);
91 values.extend(extra);
92 });
93
94 let mut config = Vec::new();
95 init.run(&mut config);
96 println!(" Final config: {:?}\n", config);
97
98 // 4. String builder pattern
99 println!("4. String builder pattern");
100 let greeting = String::from("Hello, ");
101 let name = String::from("Alice");
102 let punctuation = String::from("!");
103
104 let builder = BoxMutatorOnce::new(move |s: &mut String| {
105 println!(" Adding greeting: {}", greeting);
106 s.insert_str(0, &greeting);
107 })
108 .and_then(move |s: &mut String| {
109 println!(" Adding name: {}", name);
110 s.push_str(&name);
111 })
112 .and_then(move |s: &mut String| {
113 println!(" Adding punctuation: {}", punctuation);
114 s.push_str(&punctuation);
115 })
116 .and_then(|s: &mut String| {
117 println!(" Converting to uppercase");
118 *s = s.to_uppercase();
119 });
120
121 let mut message = String::new();
122 builder.apply(&mut message);
123 println!(" Final message: {}\n", message);
124
125 // 5. Direct closure usage
126 println!("5. Direct closure usage");
127 let data1 = vec![10, 20];
128 let data2 = vec![30, 40];
129
130 let chained_closure = (move |x: &mut Vec<i32>| {
131 println!(" Step 1: Adding {:?}", data1);
132 x.extend(data1);
133 })
134 .and_then(move |x: &mut Vec<i32>| {
135 println!(" Step 2: Adding {:?}", data2);
136 x.extend(data2);
137 })
138 .and_then(|x: &mut Vec<i32>| {
139 println!(" Step 3: Multiplying each element by 2");
140 x.iter_mut().for_each(|n| *n *= 2);
141 });
142
143 let mut values = vec![0];
144 chained_closure.apply(&mut values);
145 println!(" Result: {:?}\n", values);
146
147 // 6. Resource transfer scenario
148 println!("6. Resource transfer scenario");
149 let large_data = vec![1; 10];
150 println!(" Preparing to transfer large data (length: {})", large_data.len());
151
152 let mutator = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
153 println!(" Transferring data (moving, not cloning)");
154 x.extend(large_data); // large_data is moved, not cloned
155 });
156
157 let mut container = Vec::new();
158 mutator.apply(&mut container);
159 println!(" Data length in container: {}\n", container.len());
160
161 // 7. Generic function usage
162 println!("7. Generic function usage");
163
164 fn apply_transformation<M: MutatorOnce<Vec<i32>>>(mutator: M, initial: Vec<i32>) -> Vec<i32> {
165 let mut val = initial;
166 mutator.apply(&mut val);
167 val
168 }
169
170 let data = vec![100, 200, 300];
171 let result = apply_transformation(
172 move |x: &mut Vec<i32>| {
173 println!(" Adding in generic function: {:?}", data);
174 x.extend(data);
175 },
176 vec![0],
177 );
178 println!(" Result: {:?}\n", result);
179
180 // 8. Configuration builder
181 println!("8. Configuration builder");
182
183 struct Config {
184 options: Vec<String>,
185 }
186
187 impl Config {
188 fn new() -> Self {
189 Self { options: Vec::new() }
190 }
191
192 fn with_defaults(mut self) -> Self {
193 println!(" Adding default options");
194 self.options.push("default1".to_string());
195 self.options.push("default2".to_string());
196 self
197 }
198
199 fn customize<F>(mut self, customizer: F) -> Self
200 where
201 F: FnOnce(&mut Vec<String>) + 'static,
202 {
203 println!(" Applying custom configuration");
204 customizer.apply(&mut self.options);
205 self
206 }
207
208 fn build(self) -> Self {
209 println!(" Configuration build completed");
210 self
211 }
212 }
213
214 let custom_opts = vec!["custom1".to_string(), "custom2".to_string()];
215 let config = Config::new()
216 .with_defaults()
217 .customize(move |opts| {
218 println!(" Adding custom options: {:?}", custom_opts);
219 opts.extend(custom_opts);
220 })
221 .build();
222
223 println!(" Final options: {:?}\n", config.options);
224
225 println!("=== Examples completed ===");
226}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl<T, F> FnMutatorOnceOps<T> for F
Implements FnMutatorOnceOps for all closure types