FnMutatorOnceOps

Trait FnMutatorOnceOps 

Source
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 prism3_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.mutate(&mut target);
assert_eq!(target, vec![0, 1, 2, 3, 4]);

§Author

Haixing Hu

Provided Methods§

Source

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. Since BoxMutatorOnce cannot be cloned, the parameter will be consumed. Can be:
    • A closure: |x: &mut T|
    • A BoxMutatorOnce<T>
    • Any type implementing MutatorOnce<T>
§Returns

Returns the composed BoxMutatorOnce<T>

§Examples
use prism3_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.mutate(&mut target);
assert_eq!(target, vec![0, 1, 2, 3, 4]);
// The original closures are consumed and no longer usable
Examples found in repository?
examples/mutator_once_demo.rs (lines 129-132)
15fn main() {
16    println!("=== MutatorOnce Examples ===\n");
17
18    // 1. Basic usage: moving captured variables
19    println!("1. Basic usage: moving captured variables");
20    let data = vec![1, 2, 3];
21    let mutator = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
22        println!("   Adding data: {:?}", data);
23        x.extend(data);
24    });
25
26    let mut target = vec![0];
27    mutator.mutate(&mut target);
28    println!("   Result: {:?}\n", target);
29
30    // 2. Method chaining: combining multiple operations
31    println!("2. Method chaining: combining multiple operations");
32    let prefix = vec![1, 2];
33    let middle = vec![3, 4];
34    let suffix = vec![5, 6];
35
36    let chained = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
37        println!("   Adding prefix: {:?}", prefix);
38        x.extend(prefix);
39    })
40    .and_then(move |x: &mut Vec<i32>| {
41        println!("   Adding middle: {:?}", middle);
42        x.extend(middle);
43    })
44    .and_then(move |x: &mut Vec<i32>| {
45        println!("   Adding suffix: {:?}", suffix);
46        x.extend(suffix);
47    });
48
49    let mut result = vec![0];
50    chained.mutate(&mut result);
51    println!("   Result: {:?}\n", result);
52
53    // 3. Initializer pattern
54    println!("3. Initializer pattern");
55
56    struct Initializer {
57        name: String,
58        on_complete: Option<BoxMutatorOnce<Vec<String>>>,
59    }
60
61    impl Initializer {
62        fn new<F>(name: impl Into<String>, callback: F) -> Self
63        where
64            F: FnOnce(&mut Vec<String>) + 'static,
65        {
66            Self {
67                name: name.into(),
68                on_complete: Some(BoxMutatorOnce::new(callback)),
69            }
70        }
71
72        fn run(mut self, data: &mut Vec<String>) {
73            println!("   Initializer '{}' is running", self.name);
74            data.push(format!("Initialized by {}", self.name));
75
76            if let Some(callback) = self.on_complete.take() {
77                println!("   Executing completion callback");
78                callback.mutate(data);
79            }
80        }
81    }
82
83    let extra = vec!["extra1".to_string(), "extra2".to_string()];
84    let init = Initializer::new("MainInit", move |values| {
85        println!("   Adding extra data in callback: {:?}", extra);
86        values.extend(extra);
87    });
88
89    let mut config = Vec::new();
90    init.run(&mut config);
91    println!("   Final config: {:?}\n", config);
92
93    // 4. String builder pattern
94    println!("4. String builder pattern");
95    let greeting = String::from("Hello, ");
96    let name = String::from("Alice");
97    let punctuation = String::from("!");
98
99    let builder = BoxMutatorOnce::new(move |s: &mut String| {
100        println!("   Adding greeting: {}", greeting);
101        s.insert_str(0, &greeting);
102    })
103    .and_then(move |s: &mut String| {
104        println!("   Adding name: {}", name);
105        s.push_str(&name);
106    })
107    .and_then(move |s: &mut String| {
108        println!("   Adding punctuation: {}", punctuation);
109        s.push_str(&punctuation);
110    })
111    .and_then(|s: &mut String| {
112        println!("   Converting to uppercase");
113        *s = s.to_uppercase();
114    });
115
116    let mut message = String::new();
117    builder.mutate(&mut message);
118    println!("   Final message: {}\n", message);
119
120    // 5. Direct closure usage
121    println!("5. Direct closure usage");
122    let data1 = vec![10, 20];
123    let data2 = vec![30, 40];
124
125    let chained_closure = (move |x: &mut Vec<i32>| {
126        println!("   Step 1: Adding {:?}", data1);
127        x.extend(data1);
128    })
129    .and_then(move |x: &mut Vec<i32>| {
130        println!("   Step 2: Adding {:?}", data2);
131        x.extend(data2);
132    })
133    .and_then(|x: &mut Vec<i32>| {
134        println!("   Step 3: Multiplying each element by 2");
135        x.iter_mut().for_each(|n| *n *= 2);
136    });
137
138    let mut values = vec![0];
139    chained_closure.mutate(&mut values);
140    println!("   Result: {:?}\n", values);
141
142    // 6. Resource transfer scenario
143    println!("6. Resource transfer scenario");
144    let large_data = vec![1; 10];
145    println!(
146        "   Preparing to transfer large data (length: {})",
147        large_data.len()
148    );
149
150    let mutator = BoxMutatorOnce::new(move |x: &mut Vec<i32>| {
151        println!("   Transferring data (moving, not cloning)");
152        x.extend(large_data); // large_data is moved, not cloned
153    });
154
155    let mut container = Vec::new();
156    mutator.mutate(&mut container);
157    println!("   Data length in container: {}\n", container.len());
158
159    // 7. Generic function usage
160    println!("7. Generic function usage");
161
162    fn apply_transformation<M: MutatorOnce<Vec<i32>>>(mutator: M, initial: Vec<i32>) -> Vec<i32> {
163        let mut val = initial;
164        mutator.mutate(&mut val);
165        val
166    }
167
168    let data = vec![100, 200, 300];
169    let result = apply_transformation(
170        move |x: &mut Vec<i32>| {
171            println!("   Adding in generic function: {:?}", data);
172            x.extend(data);
173        },
174        vec![0],
175    );
176    println!("   Result: {:?}\n", result);
177
178    // 8. Configuration builder
179    println!("8. Configuration builder");
180
181    struct Config {
182        options: Vec<String>,
183    }
184
185    impl Config {
186        fn new() -> Self {
187            Self {
188                options: Vec::new(),
189            }
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.mutate(&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", so this trait is not object safe.

Implementors§

Source§

impl<T, F> FnMutatorOnceOps<T> for F
where F: FnOnce(&mut T),

Implements FnMutatorOnceOps for all closure types