pub struct BoxConsumerOnce<T> { /* private fields */ }Expand description
BoxConsumerOnce struct
One-time consumer implementation based on Box<dyn FnOnce(&T)> for single ownership scenarios.
This is the simplest consumer type for truly one-time use.
§Features
- Single Ownership: Not cloneable, transfers ownership on use
- Zero Overhead: No reference counting or lock overhead
- One-time Use: Consumes self on first call
- Builder Pattern: Method chaining naturally consumes
self
§Use Cases
Choose BoxConsumerOnce when:
- Consumer is truly used only once
- Building pipelines where ownership flows naturally
- Consumer captures values that should be consumed
- Performance critical and cannot accept shared overhead
§Performance
BoxConsumerOnce has the best performance:
- No reference counting overhead
- No lock acquisition or runtime borrow checking
- Direct function call through vtable
- Minimal memory footprint (single pointer)
§Examples
use qubit_function::{ConsumerOnce, BoxConsumerOnce};
let consumer = BoxConsumerOnce::new(|x: &i32| {
println!("Value: {}", x);
});
consumer.accept(&5);Implementations§
Source§impl<T> BoxConsumerOnce<T>
impl<T> BoxConsumerOnce<T>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new consumer.
Wraps the provided closure in the appropriate smart pointer type for this consumer implementation.
Examples found in repository?
24fn main() {
25 println!("=== ConsumerOnce Demo ===\n");
26
27 // 1. BoxConsumerOnce - Single ownership, one-time use
28 println!("1. BoxConsumerOnce - Single ownership");
29 {
30 let log = Arc::new(Mutex::new(Vec::new()));
31 let l = log.clone();
32 let consumer = BoxConsumerOnce::new(move |x: &i32| {
33 l.lock().expect("mutex should not be poisoned").push(*x);
34 println!(" BoxConsumerOnce consumed: {}", x);
35 });
36 consumer.accept(&42);
37 println!(
38 " Log: {:?}\n",
39 *log.lock().expect("mutex should not be poisoned")
40 );
41 }
42
43 // 2. BoxConsumerOnce - Method chaining
44 println!("2. BoxConsumerOnce - Method chaining");
45 {
46 let log = Arc::new(Mutex::new(Vec::new()));
47 let l1 = log.clone();
48 let l2 = log.clone();
49 let l3 = log.clone();
50 let chained = BoxConsumerOnce::new(move |x: &i32| {
51 l1.lock()
52 .expect("mutex should not be poisoned")
53 .push(*x * 2);
54 println!(" Step 1: {} * 2 = {}", x, x * 2);
55 })
56 .and_then(move |x: &i32| {
57 l2.lock()
58 .expect("mutex should not be poisoned")
59 .push(*x + 10);
60 println!(" Step 2: {} + 10 = {}", x, x + 10);
61 })
62 .and_then(move |x: &i32| {
63 l3.lock()
64 .expect("mutex should not be poisoned")
65 .push(*x - 1);
66 println!(" Step 3: {} - 1 = {}", x, x - 1);
67 });
68 chained.accept(&5);
69 println!(
70 " Log: {:?}\n",
71 *log.lock().expect("mutex should not be poisoned")
72 );
73 }
74
75 // 3. BoxConsumerOnce - Factory methods
76 println!("3. BoxConsumerOnce - Factory methods");
77 {
78 // No-op consumer
79 let noop = BoxConsumerOnce::<i32>::noop();
80 noop.accept(&42);
81 println!(" No-op consumer executed (no output)");
82
83 // Print consumer
84 print!(" Print consumer: ");
85 let print = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
86 print.accept(&42);
87
88 // Print with prefix
89 print!(" Print with prefix: ");
90 let print_with = BoxConsumerOnce::new(|x: &i32| println!("Value: {}", x));
91 print_with.accept(&42);
92
93 // Conditional consumer
94 let log = Arc::new(Mutex::new(Vec::new()));
95 let l = log.clone();
96 let conditional = BoxConsumerOnce::new(move |x: &i32| {
97 l.lock().expect("mutex should not be poisoned").push(*x * 2);
98 })
99 .when(|x: &i32| *x > 0);
100 conditional.accept(&5);
101 println!(
102 " Conditional (positive): {:?}",
103 *log.lock().expect("mutex should not be poisoned")
104 );
105
106 let log = Arc::new(Mutex::new(Vec::new()));
107 let l = log.clone();
108 let conditional = BoxConsumerOnce::new(move |x: &i32| {
109 l.lock().expect("mutex should not be poisoned").push(*x * 2);
110 })
111 .when(|x: &i32| *x > 0);
112 conditional.accept(&-5);
113 println!(
114 " Conditional (negative): {:?}\n",
115 *log.lock().expect("mutex should not be poisoned")
116 );
117 }
118
119 // 4. Closure usage
120 println!("4. Closure usage");
121 {
122 let log = Arc::new(Mutex::new(Vec::new()));
123 let l = log.clone();
124 let closure = move |x: &i32| {
125 l.lock().expect("mutex should not be poisoned").push(*x * 2);
126 println!(" Closure consumed: {}", x);
127 };
128 closure.accept(&42);
129 println!(
130 " Log: {:?}\n",
131 *log.lock().expect("mutex should not be poisoned")
132 );
133 }
134
135 // 5. Closure chaining
136 println!("5. Closure chaining");
137 {
138 let log = Arc::new(Mutex::new(Vec::new()));
139 let l1 = log.clone();
140 let l2 = log.clone();
141 let chained = (move |x: &i32| {
142 l1.lock()
143 .expect("mutex should not be poisoned")
144 .push(*x * 2);
145 println!(" Closure 1: {} * 2 = {}", x, x * 2);
146 })
147 .and_then(move |x: &i32| {
148 l2.lock()
149 .expect("mutex should not be poisoned")
150 .push(*x + 10);
151 println!(" Closure 2: {} + 10 = {}", x, x + 10);
152 });
153 chained.accept(&5);
154 println!(
155 " Log: {:?}\n",
156 *log.lock().expect("mutex should not be poisoned")
157 );
158 }
159
160 // 6. Type conversions
161 println!("6. Type conversions");
162 {
163 let log = Arc::new(Mutex::new(Vec::new()));
164
165 // Closure to BoxConsumerOnce
166 let l = log.clone();
167 let closure = move |x: &i32| {
168 l.lock().expect("mutex should not be poisoned").push(*x);
169 };
170 let box_consumer = closure.into_box();
171 box_consumer.accept(&1);
172 println!(
173 " BoxConsumerOnce: {:?}",
174 *log.lock().expect("mutex should not be poisoned")
175 );
176 }
177
178 // 7. Using with iterators (BoxConsumerOnce)
179 println!("7. Using with iterators");
180 {
181 let log = Arc::new(Mutex::new(Vec::new()));
182 let l = log.clone();
183 let consumer = BoxConsumerOnce::new(move |x: &i32| {
184 l.lock().expect("mutex should not be poisoned").push(*x * 2);
185 });
186 // Note: This will panic because BoxConsumerOnce can only be called once
187 // vec![1, 2, 3, 4, 5].iter().for_each(consumer.into_fn());
188 consumer.accept(&1);
189 println!(
190 " BoxConsumerOnce with single value: {:?}\n",
191 *log.lock().expect("mutex should not be poisoned")
192 );
193 }
194
195 println!("=== Demo Complete ===");
196}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Self
pub fn new_with_name<F>(name: &str, f: F) -> Self
Creates a new named consumer.
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>) -> Self
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
Creates a new named consumer 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 consumer.
Sourcepub fn noop() -> Self
pub fn noop() -> Self
Creates a no-operation consumer.
Creates a consumer that does nothing when called. Useful for default values or placeholder implementations.
§Returns
Returns a new consumer instance that performs no operation.
Examples found in repository?
24fn main() {
25 println!("=== ConsumerOnce Demo ===\n");
26
27 // 1. BoxConsumerOnce - Single ownership, one-time use
28 println!("1. BoxConsumerOnce - Single ownership");
29 {
30 let log = Arc::new(Mutex::new(Vec::new()));
31 let l = log.clone();
32 let consumer = BoxConsumerOnce::new(move |x: &i32| {
33 l.lock().expect("mutex should not be poisoned").push(*x);
34 println!(" BoxConsumerOnce consumed: {}", x);
35 });
36 consumer.accept(&42);
37 println!(
38 " Log: {:?}\n",
39 *log.lock().expect("mutex should not be poisoned")
40 );
41 }
42
43 // 2. BoxConsumerOnce - Method chaining
44 println!("2. BoxConsumerOnce - Method chaining");
45 {
46 let log = Arc::new(Mutex::new(Vec::new()));
47 let l1 = log.clone();
48 let l2 = log.clone();
49 let l3 = log.clone();
50 let chained = BoxConsumerOnce::new(move |x: &i32| {
51 l1.lock()
52 .expect("mutex should not be poisoned")
53 .push(*x * 2);
54 println!(" Step 1: {} * 2 = {}", x, x * 2);
55 })
56 .and_then(move |x: &i32| {
57 l2.lock()
58 .expect("mutex should not be poisoned")
59 .push(*x + 10);
60 println!(" Step 2: {} + 10 = {}", x, x + 10);
61 })
62 .and_then(move |x: &i32| {
63 l3.lock()
64 .expect("mutex should not be poisoned")
65 .push(*x - 1);
66 println!(" Step 3: {} - 1 = {}", x, x - 1);
67 });
68 chained.accept(&5);
69 println!(
70 " Log: {:?}\n",
71 *log.lock().expect("mutex should not be poisoned")
72 );
73 }
74
75 // 3. BoxConsumerOnce - Factory methods
76 println!("3. BoxConsumerOnce - Factory methods");
77 {
78 // No-op consumer
79 let noop = BoxConsumerOnce::<i32>::noop();
80 noop.accept(&42);
81 println!(" No-op consumer executed (no output)");
82
83 // Print consumer
84 print!(" Print consumer: ");
85 let print = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
86 print.accept(&42);
87
88 // Print with prefix
89 print!(" Print with prefix: ");
90 let print_with = BoxConsumerOnce::new(|x: &i32| println!("Value: {}", x));
91 print_with.accept(&42);
92
93 // Conditional consumer
94 let log = Arc::new(Mutex::new(Vec::new()));
95 let l = log.clone();
96 let conditional = BoxConsumerOnce::new(move |x: &i32| {
97 l.lock().expect("mutex should not be poisoned").push(*x * 2);
98 })
99 .when(|x: &i32| *x > 0);
100 conditional.accept(&5);
101 println!(
102 " Conditional (positive): {:?}",
103 *log.lock().expect("mutex should not be poisoned")
104 );
105
106 let log = Arc::new(Mutex::new(Vec::new()));
107 let l = log.clone();
108 let conditional = BoxConsumerOnce::new(move |x: &i32| {
109 l.lock().expect("mutex should not be poisoned").push(*x * 2);
110 })
111 .when(|x: &i32| *x > 0);
112 conditional.accept(&-5);
113 println!(
114 " Conditional (negative): {:?}\n",
115 *log.lock().expect("mutex should not be poisoned")
116 );
117 }
118
119 // 4. Closure usage
120 println!("4. Closure usage");
121 {
122 let log = Arc::new(Mutex::new(Vec::new()));
123 let l = log.clone();
124 let closure = move |x: &i32| {
125 l.lock().expect("mutex should not be poisoned").push(*x * 2);
126 println!(" Closure consumed: {}", x);
127 };
128 closure.accept(&42);
129 println!(
130 " Log: {:?}\n",
131 *log.lock().expect("mutex should not be poisoned")
132 );
133 }
134
135 // 5. Closure chaining
136 println!("5. Closure chaining");
137 {
138 let log = Arc::new(Mutex::new(Vec::new()));
139 let l1 = log.clone();
140 let l2 = log.clone();
141 let chained = (move |x: &i32| {
142 l1.lock()
143 .expect("mutex should not be poisoned")
144 .push(*x * 2);
145 println!(" Closure 1: {} * 2 = {}", x, x * 2);
146 })
147 .and_then(move |x: &i32| {
148 l2.lock()
149 .expect("mutex should not be poisoned")
150 .push(*x + 10);
151 println!(" Closure 2: {} + 10 = {}", x, x + 10);
152 });
153 chained.accept(&5);
154 println!(
155 " Log: {:?}\n",
156 *log.lock().expect("mutex should not be poisoned")
157 );
158 }
159
160 // 6. Type conversions
161 println!("6. Type conversions");
162 {
163 let log = Arc::new(Mutex::new(Vec::new()));
164
165 // Closure to BoxConsumerOnce
166 let l = log.clone();
167 let closure = move |x: &i32| {
168 l.lock().expect("mutex should not be poisoned").push(*x);
169 };
170 let box_consumer = closure.into_box();
171 box_consumer.accept(&1);
172 println!(
173 " BoxConsumerOnce: {:?}",
174 *log.lock().expect("mutex should not be poisoned")
175 );
176 }
177
178 // 7. Using with iterators (BoxConsumerOnce)
179 println!("7. Using with iterators");
180 {
181 let log = Arc::new(Mutex::new(Vec::new()));
182 let l = log.clone();
183 let consumer = BoxConsumerOnce::new(move |x: &i32| {
184 l.lock().expect("mutex should not be poisoned").push(*x * 2);
185 });
186 // Note: This will panic because BoxConsumerOnce can only be called once
187 // vec![1, 2, 3, 4, 5].iter().for_each(consumer.into_fn());
188 consumer.accept(&1);
189 println!(
190 " BoxConsumerOnce with single value: {:?}\n",
191 *log.lock().expect("mutex should not be poisoned")
192 );
193 }
194
195 println!("=== Demo Complete ===");
196}Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalConsumerOnce<T>where
T: 'static,
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalConsumerOnce<T>where
T: 'static,
P: Predicate<T> + 'static,
Creates a conditional consumer that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the consumption operation
§Returns
Returns a conditional consumer that only executes when the
predicate returns true.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use qubit_function::consumers::*;
let counter = Arc::new(AtomicI32::new(0));
let consumer = BoxConsumer::new({
let counter = Arc::clone(&counter);
move |value: &i32| {
counter.fetch_add(*value, Ordering::SeqCst);
}
});
let conditional = consumer.when(|value: &i32| *value > 0);
conditional.accept(&1); // counter = 1
conditional.accept(&-1); // not executed, counter remains 1Examples found in repository?
24fn main() {
25 println!("=== ConsumerOnce Demo ===\n");
26
27 // 1. BoxConsumerOnce - Single ownership, one-time use
28 println!("1. BoxConsumerOnce - Single ownership");
29 {
30 let log = Arc::new(Mutex::new(Vec::new()));
31 let l = log.clone();
32 let consumer = BoxConsumerOnce::new(move |x: &i32| {
33 l.lock().expect("mutex should not be poisoned").push(*x);
34 println!(" BoxConsumerOnce consumed: {}", x);
35 });
36 consumer.accept(&42);
37 println!(
38 " Log: {:?}\n",
39 *log.lock().expect("mutex should not be poisoned")
40 );
41 }
42
43 // 2. BoxConsumerOnce - Method chaining
44 println!("2. BoxConsumerOnce - Method chaining");
45 {
46 let log = Arc::new(Mutex::new(Vec::new()));
47 let l1 = log.clone();
48 let l2 = log.clone();
49 let l3 = log.clone();
50 let chained = BoxConsumerOnce::new(move |x: &i32| {
51 l1.lock()
52 .expect("mutex should not be poisoned")
53 .push(*x * 2);
54 println!(" Step 1: {} * 2 = {}", x, x * 2);
55 })
56 .and_then(move |x: &i32| {
57 l2.lock()
58 .expect("mutex should not be poisoned")
59 .push(*x + 10);
60 println!(" Step 2: {} + 10 = {}", x, x + 10);
61 })
62 .and_then(move |x: &i32| {
63 l3.lock()
64 .expect("mutex should not be poisoned")
65 .push(*x - 1);
66 println!(" Step 3: {} - 1 = {}", x, x - 1);
67 });
68 chained.accept(&5);
69 println!(
70 " Log: {:?}\n",
71 *log.lock().expect("mutex should not be poisoned")
72 );
73 }
74
75 // 3. BoxConsumerOnce - Factory methods
76 println!("3. BoxConsumerOnce - Factory methods");
77 {
78 // No-op consumer
79 let noop = BoxConsumerOnce::<i32>::noop();
80 noop.accept(&42);
81 println!(" No-op consumer executed (no output)");
82
83 // Print consumer
84 print!(" Print consumer: ");
85 let print = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
86 print.accept(&42);
87
88 // Print with prefix
89 print!(" Print with prefix: ");
90 let print_with = BoxConsumerOnce::new(|x: &i32| println!("Value: {}", x));
91 print_with.accept(&42);
92
93 // Conditional consumer
94 let log = Arc::new(Mutex::new(Vec::new()));
95 let l = log.clone();
96 let conditional = BoxConsumerOnce::new(move |x: &i32| {
97 l.lock().expect("mutex should not be poisoned").push(*x * 2);
98 })
99 .when(|x: &i32| *x > 0);
100 conditional.accept(&5);
101 println!(
102 " Conditional (positive): {:?}",
103 *log.lock().expect("mutex should not be poisoned")
104 );
105
106 let log = Arc::new(Mutex::new(Vec::new()));
107 let l = log.clone();
108 let conditional = BoxConsumerOnce::new(move |x: &i32| {
109 l.lock().expect("mutex should not be poisoned").push(*x * 2);
110 })
111 .when(|x: &i32| *x > 0);
112 conditional.accept(&-5);
113 println!(
114 " Conditional (negative): {:?}\n",
115 *log.lock().expect("mutex should not be poisoned")
116 );
117 }
118
119 // 4. Closure usage
120 println!("4. Closure usage");
121 {
122 let log = Arc::new(Mutex::new(Vec::new()));
123 let l = log.clone();
124 let closure = move |x: &i32| {
125 l.lock().expect("mutex should not be poisoned").push(*x * 2);
126 println!(" Closure consumed: {}", x);
127 };
128 closure.accept(&42);
129 println!(
130 " Log: {:?}\n",
131 *log.lock().expect("mutex should not be poisoned")
132 );
133 }
134
135 // 5. Closure chaining
136 println!("5. Closure chaining");
137 {
138 let log = Arc::new(Mutex::new(Vec::new()));
139 let l1 = log.clone();
140 let l2 = log.clone();
141 let chained = (move |x: &i32| {
142 l1.lock()
143 .expect("mutex should not be poisoned")
144 .push(*x * 2);
145 println!(" Closure 1: {} * 2 = {}", x, x * 2);
146 })
147 .and_then(move |x: &i32| {
148 l2.lock()
149 .expect("mutex should not be poisoned")
150 .push(*x + 10);
151 println!(" Closure 2: {} + 10 = {}", x, x + 10);
152 });
153 chained.accept(&5);
154 println!(
155 " Log: {:?}\n",
156 *log.lock().expect("mutex should not be poisoned")
157 );
158 }
159
160 // 6. Type conversions
161 println!("6. Type conversions");
162 {
163 let log = Arc::new(Mutex::new(Vec::new()));
164
165 // Closure to BoxConsumerOnce
166 let l = log.clone();
167 let closure = move |x: &i32| {
168 l.lock().expect("mutex should not be poisoned").push(*x);
169 };
170 let box_consumer = closure.into_box();
171 box_consumer.accept(&1);
172 println!(
173 " BoxConsumerOnce: {:?}",
174 *log.lock().expect("mutex should not be poisoned")
175 );
176 }
177
178 // 7. Using with iterators (BoxConsumerOnce)
179 println!("7. Using with iterators");
180 {
181 let log = Arc::new(Mutex::new(Vec::new()));
182 let l = log.clone();
183 let consumer = BoxConsumerOnce::new(move |x: &i32| {
184 l.lock().expect("mutex should not be poisoned").push(*x * 2);
185 });
186 // Note: This will panic because BoxConsumerOnce can only be called once
187 // vec![1, 2, 3, 4, 5].iter().for_each(consumer.into_fn());
188 consumer.accept(&1);
189 println!(
190 " BoxConsumerOnce with single value: {:?}\n",
191 *log.lock().expect("mutex should not be poisoned")
192 );
193 }
194
195 println!("=== Demo Complete ===");
196}Sourcepub fn and_then<C>(self, after: C) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
T: 'static,
C: ConsumerOnce<T> + 'static,
pub fn and_then<C>(self, after: C) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
T: 'static,
C: ConsumerOnce<T> + 'static,
Chains execution with another consumer, executing the current consumer first, then the subsequent consumer.
§Parameters
after- The subsequent consumer to execute after the current consumer completes
§Returns
Returns a new consumer that executes the current consumer and the subsequent consumer in sequence.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use qubit_function::consumers::*;
let counter1 = Arc::new(AtomicI32::new(0));
let counter2 = Arc::new(AtomicI32::new(0));
let consumer1 = BoxConsumer::new({
let counter = Arc::clone(&counter1);
move |value: &i32| {
counter.fetch_add(*value, Ordering::SeqCst);
}
});
let consumer2 = BoxConsumer::new({
let counter = Arc::clone(&counter2);
move |value: &i32| {
counter.fetch_add(*value * 2, Ordering::SeqCst);
}
});
let chained = consumer1.and_then(consumer2);
chained.accept(&5);
// counter1 = 5, counter2 = 10Examples found in repository?
24fn main() {
25 println!("=== ConsumerOnce Demo ===\n");
26
27 // 1. BoxConsumerOnce - Single ownership, one-time use
28 println!("1. BoxConsumerOnce - Single ownership");
29 {
30 let log = Arc::new(Mutex::new(Vec::new()));
31 let l = log.clone();
32 let consumer = BoxConsumerOnce::new(move |x: &i32| {
33 l.lock().expect("mutex should not be poisoned").push(*x);
34 println!(" BoxConsumerOnce consumed: {}", x);
35 });
36 consumer.accept(&42);
37 println!(
38 " Log: {:?}\n",
39 *log.lock().expect("mutex should not be poisoned")
40 );
41 }
42
43 // 2. BoxConsumerOnce - Method chaining
44 println!("2. BoxConsumerOnce - Method chaining");
45 {
46 let log = Arc::new(Mutex::new(Vec::new()));
47 let l1 = log.clone();
48 let l2 = log.clone();
49 let l3 = log.clone();
50 let chained = BoxConsumerOnce::new(move |x: &i32| {
51 l1.lock()
52 .expect("mutex should not be poisoned")
53 .push(*x * 2);
54 println!(" Step 1: {} * 2 = {}", x, x * 2);
55 })
56 .and_then(move |x: &i32| {
57 l2.lock()
58 .expect("mutex should not be poisoned")
59 .push(*x + 10);
60 println!(" Step 2: {} + 10 = {}", x, x + 10);
61 })
62 .and_then(move |x: &i32| {
63 l3.lock()
64 .expect("mutex should not be poisoned")
65 .push(*x - 1);
66 println!(" Step 3: {} - 1 = {}", x, x - 1);
67 });
68 chained.accept(&5);
69 println!(
70 " Log: {:?}\n",
71 *log.lock().expect("mutex should not be poisoned")
72 );
73 }
74
75 // 3. BoxConsumerOnce - Factory methods
76 println!("3. BoxConsumerOnce - Factory methods");
77 {
78 // No-op consumer
79 let noop = BoxConsumerOnce::<i32>::noop();
80 noop.accept(&42);
81 println!(" No-op consumer executed (no output)");
82
83 // Print consumer
84 print!(" Print consumer: ");
85 let print = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
86 print.accept(&42);
87
88 // Print with prefix
89 print!(" Print with prefix: ");
90 let print_with = BoxConsumerOnce::new(|x: &i32| println!("Value: {}", x));
91 print_with.accept(&42);
92
93 // Conditional consumer
94 let log = Arc::new(Mutex::new(Vec::new()));
95 let l = log.clone();
96 let conditional = BoxConsumerOnce::new(move |x: &i32| {
97 l.lock().expect("mutex should not be poisoned").push(*x * 2);
98 })
99 .when(|x: &i32| *x > 0);
100 conditional.accept(&5);
101 println!(
102 " Conditional (positive): {:?}",
103 *log.lock().expect("mutex should not be poisoned")
104 );
105
106 let log = Arc::new(Mutex::new(Vec::new()));
107 let l = log.clone();
108 let conditional = BoxConsumerOnce::new(move |x: &i32| {
109 l.lock().expect("mutex should not be poisoned").push(*x * 2);
110 })
111 .when(|x: &i32| *x > 0);
112 conditional.accept(&-5);
113 println!(
114 " Conditional (negative): {:?}\n",
115 *log.lock().expect("mutex should not be poisoned")
116 );
117 }
118
119 // 4. Closure usage
120 println!("4. Closure usage");
121 {
122 let log = Arc::new(Mutex::new(Vec::new()));
123 let l = log.clone();
124 let closure = move |x: &i32| {
125 l.lock().expect("mutex should not be poisoned").push(*x * 2);
126 println!(" Closure consumed: {}", x);
127 };
128 closure.accept(&42);
129 println!(
130 " Log: {:?}\n",
131 *log.lock().expect("mutex should not be poisoned")
132 );
133 }
134
135 // 5. Closure chaining
136 println!("5. Closure chaining");
137 {
138 let log = Arc::new(Mutex::new(Vec::new()));
139 let l1 = log.clone();
140 let l2 = log.clone();
141 let chained = (move |x: &i32| {
142 l1.lock()
143 .expect("mutex should not be poisoned")
144 .push(*x * 2);
145 println!(" Closure 1: {} * 2 = {}", x, x * 2);
146 })
147 .and_then(move |x: &i32| {
148 l2.lock()
149 .expect("mutex should not be poisoned")
150 .push(*x + 10);
151 println!(" Closure 2: {} + 10 = {}", x, x + 10);
152 });
153 chained.accept(&5);
154 println!(
155 " Log: {:?}\n",
156 *log.lock().expect("mutex should not be poisoned")
157 );
158 }
159
160 // 6. Type conversions
161 println!("6. Type conversions");
162 {
163 let log = Arc::new(Mutex::new(Vec::new()));
164
165 // Closure to BoxConsumerOnce
166 let l = log.clone();
167 let closure = move |x: &i32| {
168 l.lock().expect("mutex should not be poisoned").push(*x);
169 };
170 let box_consumer = closure.into_box();
171 box_consumer.accept(&1);
172 println!(
173 " BoxConsumerOnce: {:?}",
174 *log.lock().expect("mutex should not be poisoned")
175 );
176 }
177
178 // 7. Using with iterators (BoxConsumerOnce)
179 println!("7. Using with iterators");
180 {
181 let log = Arc::new(Mutex::new(Vec::new()));
182 let l = log.clone();
183 let consumer = BoxConsumerOnce::new(move |x: &i32| {
184 l.lock().expect("mutex should not be poisoned").push(*x * 2);
185 });
186 // Note: This will panic because BoxConsumerOnce can only be called once
187 // vec![1, 2, 3, 4, 5].iter().for_each(consumer.into_fn());
188 consumer.accept(&1);
189 println!(
190 " BoxConsumerOnce with single value: {:?}\n",
191 *log.lock().expect("mutex should not be poisoned")
192 );
193 }
194
195 println!("=== Demo Complete ===");
196}