pub trait ConsumerOnce<T> {
// Required method
fn accept(self, value: &T);
// Provided methods
fn into_box(self) -> BoxConsumerOnce<T>
where Self: Sized + 'static { ... }
fn into_fn(self) -> impl FnOnce(&T)
where Self: Sized + 'static { ... }
fn to_box(&self) -> BoxConsumerOnce<T>
where Self: Sized + Clone + 'static { ... }
fn to_fn(&self) -> impl FnOnce(&T)
where Self: Sized + Clone + 'static { ... }
}Expand description
ConsumerOnce trait - Unified one-time consumer interface
It is similar to the FnOnce(&T) trait in the standard library.
Defines the core behavior of all one-time consumer types. Similar to consumers
implementing FnOnce(&T), executes operations that accept a value reference but
return no result (only side effects), consuming itself in the process.
§Automatic Implementation
- All closures implementing
FnOnce(&T) BoxConsumerOnce<T>
§Features
- Unified Interface: All consumer types share the same
acceptmethod signature - Automatic Implementation: Closures automatically implement this trait with zero overhead
- Type Conversion: Can be converted to BoxConsumerOnce
- Generic Programming: Write functions that work with any one-time consumer type
§Examples
use qubit_function::{ConsumerOnce, BoxConsumerOnce};
use std::sync::{Arc, Mutex};
fn apply_consumer<C: ConsumerOnce<i32>>(consumer: C, value: &i32) {
consumer.accept(value);
}
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let box_con = BoxConsumerOnce::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
apply_consumer(box_con, &5);
assert_eq!(*log.lock().unwrap(), vec![5]);Required Methods§
Sourcefn accept(self, value: &T)
fn accept(self, value: &T)
Execute one-time consumption operation
Executes an operation on the given reference. The operation typically reads the input value or produces side effects, but does not modify the input value itself. Consumes self.
§Parameters
value- Reference to the value to be consumed
§Examples
use qubit_function::{ConsumerOnce, BoxConsumerOnce};
let consumer = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
consumer.accept(&5);Provided Methods§
Sourcefn into_box(self) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
fn into_box(self) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
Convert to BoxConsumerOnce
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
§Default Implementation
The default implementation wraps self in a BoxConsumerOnce by calling
accept on the consumer. Types can override this method to provide more
efficient conversions.
§Returns
Returns the wrapped BoxConsumerOnce<T>
§Examples
use qubit_function::ConsumerOnce;
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
l.lock().unwrap().push(*x);
};
let box_consumer = closure.into_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);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().unwrap().push(*x);
34 println!(" BoxConsumerOnce consumed: {}", x);
35 });
36 consumer.accept(&42);
37 println!(" Log: {:?}\n", *log.lock().unwrap());
38 }
39
40 // 2. BoxConsumerOnce - Method chaining
41 println!("2. BoxConsumerOnce - Method chaining");
42 {
43 let log = Arc::new(Mutex::new(Vec::new()));
44 let l1 = log.clone();
45 let l2 = log.clone();
46 let l3 = log.clone();
47 let chained = BoxConsumerOnce::new(move |x: &i32| {
48 l1.lock().unwrap().push(*x * 2);
49 println!(" Step 1: {} * 2 = {}", x, x * 2);
50 })
51 .and_then(move |x: &i32| {
52 l2.lock().unwrap().push(*x + 10);
53 println!(" Step 2: {} + 10 = {}", x, x + 10);
54 })
55 .and_then(move |x: &i32| {
56 l3.lock().unwrap().push(*x - 1);
57 println!(" Step 3: {} - 1 = {}", x, x - 1);
58 });
59 chained.accept(&5);
60 println!(" Log: {:?}\n", *log.lock().unwrap());
61 }
62
63 // 3. BoxConsumerOnce - Factory methods
64 println!("3. BoxConsumerOnce - Factory methods");
65 {
66 // No-op consumer
67 let noop = BoxConsumerOnce::<i32>::noop();
68 noop.accept(&42);
69 println!(" No-op consumer executed (no output)");
70
71 // Print consumer
72 print!(" Print consumer: ");
73 let print = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
74 print.accept(&42);
75
76 // Print with prefix
77 print!(" Print with prefix: ");
78 let print_with = BoxConsumerOnce::new(|x: &i32| println!("Value: {}", x));
79 print_with.accept(&42);
80
81 // Conditional consumer
82 let log = Arc::new(Mutex::new(Vec::new()));
83 let l = log.clone();
84 let conditional = BoxConsumerOnce::new(move |x: &i32| {
85 l.lock().unwrap().push(*x * 2);
86 })
87 .when(|x: &i32| *x > 0);
88 conditional.accept(&5);
89 println!(" Conditional (positive): {:?}", *log.lock().unwrap());
90
91 let log = Arc::new(Mutex::new(Vec::new()));
92 let l = log.clone();
93 let conditional = BoxConsumerOnce::new(move |x: &i32| {
94 l.lock().unwrap().push(*x * 2);
95 })
96 .when(|x: &i32| *x > 0);
97 conditional.accept(&-5);
98 println!(" Conditional (negative): {:?}\n", *log.lock().unwrap());
99 }
100
101 // 4. Closure usage
102 println!("4. Closure usage");
103 {
104 let log = Arc::new(Mutex::new(Vec::new()));
105 let l = log.clone();
106 let closure = move |x: &i32| {
107 l.lock().unwrap().push(*x * 2);
108 println!(" Closure consumed: {}", x);
109 };
110 closure.accept(&42);
111 println!(" Log: {:?}\n", *log.lock().unwrap());
112 }
113
114 // 5. Closure chaining
115 println!("5. Closure chaining");
116 {
117 let log = Arc::new(Mutex::new(Vec::new()));
118 let l1 = log.clone();
119 let l2 = log.clone();
120 let chained = (move |x: &i32| {
121 l1.lock().unwrap().push(*x * 2);
122 println!(" Closure 1: {} * 2 = {}", x, x * 2);
123 })
124 .and_then(move |x: &i32| {
125 l2.lock().unwrap().push(*x + 10);
126 println!(" Closure 2: {} + 10 = {}", x, x + 10);
127 });
128 chained.accept(&5);
129 println!(" Log: {:?}\n", *log.lock().unwrap());
130 }
131
132 // 6. Type conversions
133 println!("6. Type conversions");
134 {
135 let log = Arc::new(Mutex::new(Vec::new()));
136
137 // Closure to BoxConsumerOnce
138 let l = log.clone();
139 let closure = move |x: &i32| {
140 l.lock().unwrap().push(*x);
141 };
142 let box_consumer = closure.into_box();
143 box_consumer.accept(&1);
144 println!(" BoxConsumerOnce: {:?}", *log.lock().unwrap());
145 }
146
147 // 7. Using with iterators (BoxConsumerOnce)
148 println!("7. Using with iterators");
149 {
150 let log = Arc::new(Mutex::new(Vec::new()));
151 let l = log.clone();
152 let consumer = BoxConsumerOnce::new(move |x: &i32| {
153 l.lock().unwrap().push(*x * 2);
154 });
155 // Note: This will panic because BoxConsumerOnce can only be called once
156 // vec![1, 2, 3, 4, 5].iter().for_each(consumer.into_fn());
157 consumer.accept(&1);
158 println!(
159 " BoxConsumerOnce with single value: {:?}\n",
160 *log.lock().unwrap()
161 );
162 }
163
164 println!("=== Demo Complete ===");
165}Sourcefn into_fn(self) -> impl FnOnce(&T)where
Self: Sized + 'static,
fn into_fn(self) -> impl FnOnce(&T)where
Self: Sized + 'static,
Convert to closure
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
Converts a one-time consumer to a closure that can be used directly in places
where the standard library requires FnOnce.
§Default Implementation
The default implementation creates a closure that captures self and calls
its accept method. Types can override this method to provide more efficient
conversions.
§Returns
Returns a closure implementing FnOnce(&T)
§Examples
use qubit_function::ConsumerOnce;
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
l.lock().unwrap().push(*x * 2);
};
let func = closure.into_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![10]);Sourcefn to_box(&self) -> BoxConsumerOnce<T>
fn to_box(&self) -> BoxConsumerOnce<T>
Convert to BoxConsumerOnce without consuming self
⚠️ Requires Clone: This method requires Self to implement
Clone. Clones the current consumer and wraps it in a
BoxConsumerOnce.
§Default Implementation
The default implementation clones self and then calls
into_box() on the clone. Types can override this method to
provide more efficient conversions.
§Returns
Returns the wrapped BoxConsumerOnce<T>
§Examples
use qubit_function::ConsumerOnce;
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
l.lock().unwrap().push(*x);
};
let box_consumer = closure.to_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);Sourcefn to_fn(&self) -> impl FnOnce(&T)
fn to_fn(&self) -> impl FnOnce(&T)
Convert to closure without consuming self
⚠️ Requires Clone: This method requires Self to implement
Clone. Clones the current consumer and then converts the clone
to a closure.
§Default Implementation
The default implementation clones self and then calls
into_fn() on the clone. Types can override this method to
provide more efficient conversions.
§Returns
Returns a closure implementing FnOnce(&T)
§Examples
use qubit_function::ConsumerOnce;
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
l.lock().unwrap().push(*x * 2);
};
let func = closure.to_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![10]);