pub struct BoxSupplier<T> { /* private fields */ }Expand description
Box-based single ownership stateless supplier.
Uses Box<dyn Fn() -> T> for single ownership scenarios. This
is the most lightweight stateless supplier with zero reference
counting overhead.
§Ownership Model
Methods consume self (move semantics) or borrow &self for
read-only operations. When you call methods like map(), the
original supplier is consumed and you get a new one:
use qubit_function::{BoxSupplier, Supplier};
let supplier = BoxSupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here§Examples
§Constant Factory
use qubit_function::{BoxSupplier, Supplier};
let factory = BoxSupplier::new(|| 42);
assert_eq!(factory.get(), 42);
assert_eq!(factory.get(), 42);§Method Chaining
use qubit_function::{BoxSupplier, Supplier};
let pipeline = BoxSupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(pipeline.get(), 25);Implementations§
Source§impl<T> BoxSupplier<T>
impl<T> BoxSupplier<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: Fn() -> T + 'static,
Creates a new supplier.
Wraps the provided closure in the appropriate smart pointer type for this supplier implementation.
Examples found in repository?
71fn demo_box_supplier_basics() {
72 println!("--- BoxSupplier Basics ---");
73
74 // Basic usage (Fn)
75 let supplier = BoxSupplier::new(|| 42);
76 println!("Basic: {}", supplier.get());
77
78 // Constant supplier (Fn)
79 let constant = BoxSupplier::constant(100);
80 println!("Constant: {}", constant.get());
81 println!("Constant: {}", constant.get());
82
83 // Stateful counter (FnMut) - use BoxStatefulSupplier
84 let mut counter = 0;
85 let mut counter_supplier = BoxStatefulSupplier::new(move || {
86 counter += 1;
87 counter
88 });
89 println!("Counter: {}", counter_supplier.get());
90 println!("Counter: {}", counter_supplier.get());
91 println!("Counter: {}", counter_supplier.get());
92 println!();
93}
94
95fn demo_box_supplier_methods() {
96 println!("--- BoxStatefulSupplier Methods ---");
97
98 // Map (FnMut)
99 let mut counter = 0;
100 let mut mapped = BoxStatefulSupplier::new(move || {
101 counter += 1;
102 counter * 10
103 })
104 .map(|x| x + 5);
105 println!("Mapped: {}", mapped.get());
106 println!("Mapped: {}", mapped.get());
107
108 // Filter (FnMut)
109 let mut counter = 0;
110 let mut filtered = BoxStatefulSupplier::new(move || {
111 counter += 1;
112 counter
113 })
114 .filter(is_even_i32);
115 println!("Filtered (odd): {:?}", filtered.get());
116 println!("Filtered (even): {:?}", filtered.get());
117
118 // Zip (Fn)
119 let first = BoxSupplier::new(|| 42);
120 let second = BoxSupplier::new(|| "hello");
121 let zipped = first.zip(second);
122 println!("Zipped: {:?}", zipped.get());
123
124 // Memoize (FnMut)
125 let mut call_count = 0;
126 let mut memoized = BoxStatefulSupplier::new(move || {
127 call_count += 1;
128 println!(" Expensive computation #{}", call_count);
129 42
130 })
131 .memoize();
132 println!("First call: {}", memoized.get());
133 println!("Second call (cached): {}", memoized.get());
134 println!();
135}
136
137fn demo_box_supplier_once() {
138 println!("--- BoxSupplierOnce ---");
139
140 // Basic usage
141 let once = BoxSupplierOnce::new(|| {
142 println!(" Expensive initialization");
143 42
144 });
145 println!("Value: {}", once.get());
146
147 // Moving captured values
148 let data = String::from("Hello, World!");
149 let once = BoxSupplierOnce::new(move || data);
150 println!("Moved data: {}", once.get());
151 println!();
152}
153
154fn demo_arc_supplier() {
155 println!("--- ArcSupplier ---");
156
157 // Basic usage (Fn)
158 let supplier = ArcSupplier::new(|| 42);
159 let s = supplier;
160 println!("Basic: {}", s.get());
161
162 // Reusable transformations (Fn)
163 let source = ArcSupplier::new(|| 10);
164 let doubled = source.map(|x| x * 2);
165 let tripled = source.map(|x| x * 3);
166
167 let s = source;
168 let d = doubled;
169 let t = tripled;
170 println!("Source: {}", s.get());
171 println!("Doubled: {}", d.get());
172 println!("Tripled: {}", t.get());
173
174 // Stateful with ArcStatefulSupplier (FnMut)
175 let call_count = Arc::new(Mutex::new(0));
176 let call_count_clone = Arc::clone(&call_count);
177 let source = ArcStatefulSupplier::new(move || {
178 let mut c = call_count_clone.lock().unwrap();
179 *c += 1;
180 println!(" Computation #{}", *c);
181 42
182 });
183
184 // Memoization with ArcStatefulSupplier
185 let memoized = source.memoize();
186 let mut m = memoized;
187 println!("First call: {}", m.get());
188 println!("Second call (cached): {}", m.get());
189 println!("Call count: {}", *call_count.lock().unwrap());
190 println!();
191}
192
193fn demo_arc_supplier_threading() {
194 println!("--- ArcStatefulSupplier Threading ---");
195
196 let counter = Arc::new(Mutex::new(0));
197 let counter_clone = Arc::clone(&counter);
198
199 let supplier = ArcStatefulSupplier::new(move || {
200 let mut c = counter_clone.lock().unwrap();
201 *c += 1;
202 *c
203 });
204
205 let mut s1 = supplier.clone();
206 let mut s2 = supplier.clone();
207 let mut s3 = supplier;
208
209 let h1 = thread::spawn(move || {
210 let v1 = s1.get();
211 let v2 = s1.get();
212 println!("Thread 1: {} {}", v1, v2);
213 (v1, v2)
214 });
215
216 let h2 = thread::spawn(move || {
217 let v1 = s2.get();
218 let v2 = s2.get();
219 println!("Thread 2: {} {}", v1, v2);
220 (v1, v2)
221 });
222
223 let v1 = s3.get();
224 let v2 = s3.get();
225 println!("Main thread: {} {}", v1, v2);
226
227 h1.join().unwrap();
228 h2.join().unwrap();
229
230 println!("Final counter: {}", *counter.lock().unwrap());
231 println!();
232}
233
234fn demo_rc_supplier() {
235 println!("--- RcSupplier ---");
236
237 // Basic usage (Fn)
238 let supplier = RcSupplier::new(|| 42);
239 let s = supplier;
240 println!("Basic: {}", s.get());
241
242 // Shared state (FnMut) - use RcStatefulSupplier
243 let counter = Rc::new(RefCell::new(0));
244 let counter_clone = Rc::clone(&counter);
245 let supplier = RcStatefulSupplier::new(move || {
246 let mut c = counter_clone.borrow_mut();
247 *c += 1;
248 *c
249 });
250
251 let mut s1 = supplier.clone();
252 let mut s2 = supplier.clone();
253
254 println!("First clone: {}", s1.get());
255 println!("Second clone: {}", s2.get());
256 println!("First clone again: {}", s1.get());
257
258 // Reusable transformations (Fn)
259 let source = RcSupplier::new(|| 10);
260 let doubled = source.map(|x| x * 2);
261 let tripled = source.map(|x| x * 3);
262 let squared = source.map(|x| x * x);
263
264 let s = source;
265 let d = doubled;
266 let t = tripled;
267 let sq = squared;
268
269 println!("Source: {}", s.get());
270 println!("Doubled: {}", d.get());
271 println!("Tripled: {}", t.get());
272 println!("Squared: {}", sq.get());
273 println!();
274}
275
276fn demo_type_conversions() {
277 println!("--- Type Conversions ---");
278
279 // Closure to Box (Fn)
280 let closure = || 42;
281 let boxed = Supplier::into_box(closure);
282 println!("Closure -> Box: {}", boxed.get());
283
284 // Closure to Rc (Fn)
285 let closure = || 100;
286 let rc = Supplier::into_rc(closure);
287 println!("Closure -> Rc: {}", rc.get());
288
289 // Closure to Arc (Fn)
290 let closure = || 200;
291 let arc = Supplier::into_arc(closure);
292 println!("Closure -> Arc: {}", arc.get());
293
294 // Box to Rc (Fn)
295 let boxed = BoxSupplier::new(|| 42);
296 let rc = boxed.into_rc();
297 println!("Box -> Rc: {}", rc.get());
298
299 // Arc to Box (Fn)
300 let arc = ArcSupplier::new(|| 42);
301 let boxed = arc.into_box();
302 println!("Arc -> Box: {}", boxed.get());
303
304 // Rc to Box (Fn)
305 let rc = RcSupplier::new(|| 42);
306 let boxed = rc.into_box();
307 println!("Rc -> Box: {}", boxed.get());
308
309 println!();
310}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn() -> T + 'static,
Creates a new named supplier.
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>) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn() -> T + 'static,
Creates a new named supplier 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 supplier.
Sourcepub fn constant(value: T) -> Selfwhere
T: Clone + 'static,
pub fn constant(value: T) -> Selfwhere
T: Clone + 'static,
Creates a supplier that returns a constant value.
Creates a supplier that always returns the same value. Useful for default values or placeholder implementations.
§Parameters
value- The constant value to return
§Returns
Returns a new supplier instance that returns the constant value.
Examples found in repository?
71fn demo_box_supplier_basics() {
72 println!("--- BoxSupplier Basics ---");
73
74 // Basic usage (Fn)
75 let supplier = BoxSupplier::new(|| 42);
76 println!("Basic: {}", supplier.get());
77
78 // Constant supplier (Fn)
79 let constant = BoxSupplier::constant(100);
80 println!("Constant: {}", constant.get());
81 println!("Constant: {}", constant.get());
82
83 // Stateful counter (FnMut) - use BoxStatefulSupplier
84 let mut counter = 0;
85 let mut counter_supplier = BoxStatefulSupplier::new(move || {
86 counter += 1;
87 counter
88 });
89 println!("Counter: {}", counter_supplier.get());
90 println!("Counter: {}", counter_supplier.get());
91 println!("Counter: {}", counter_supplier.get());
92 println!();
93}Sourcepub fn map<U, M>(self, mapper: M) -> BoxSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(self, mapper: M) -> BoxSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
Maps the output using a transformation function.
Consumes self and returns a new supplier that applies the mapper to each output.
§Parameters
mapper- The transformer to apply to the output. Can be a closure, function pointer, or any type implementingTransformer<T, U>.
§Returns
A new mapped supplier
§Examples
use qubit_function::suppliers::*;
let supplier = BoxSupplier::new(|| 10);
let mapped = supplier
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(mapped.get(), 25);Sourcepub fn filter<P>(self, predicate: P) -> BoxSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(self, predicate: P) -> BoxSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
Filters output based on a predicate.
Returns a new supplier that returns Some(value) if the
predicate is satisfied, None otherwise.
§Parameters
predicate- The predicate to test the supplied value
§Returns
A new filtered supplier
§Examples
use qubit_function::predicates::BoxPredicate;
use qubit_function::{Predicate, suppliers::*};
let supplier = BoxSupplier::new(|| 42);
let is_even = BoxPredicate::new(|x: &i32| *x % 2 == 0);
let filtered = supplier.filter(is_even);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U, S>(self, other: S) -> BoxSupplier<(T, U)>where
T: 'static,
S: Supplier<U> + 'static,
U: 'static,
pub fn zip<U, S>(self, other: S) -> BoxSupplier<(T, U)>where
T: 'static,
S: Supplier<U> + 'static,
U: 'static,
Combines this supplier with another, producing a tuple.
Consumes both suppliers and returns a new supplier that produces tuples.
§Parameters
other- The other supplier to combine with
§Returns
A new supplier that produces tuples
§Examples
use qubit_function::suppliers::*;
let first = BoxSupplier::new(|| 42);
let second = BoxSupplier::new(|| "hello");
let zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));Examples found in repository?
95fn demo_box_supplier_methods() {
96 println!("--- BoxStatefulSupplier Methods ---");
97
98 // Map (FnMut)
99 let mut counter = 0;
100 let mut mapped = BoxStatefulSupplier::new(move || {
101 counter += 1;
102 counter * 10
103 })
104 .map(|x| x + 5);
105 println!("Mapped: {}", mapped.get());
106 println!("Mapped: {}", mapped.get());
107
108 // Filter (FnMut)
109 let mut counter = 0;
110 let mut filtered = BoxStatefulSupplier::new(move || {
111 counter += 1;
112 counter
113 })
114 .filter(is_even_i32);
115 println!("Filtered (odd): {:?}", filtered.get());
116 println!("Filtered (even): {:?}", filtered.get());
117
118 // Zip (Fn)
119 let first = BoxSupplier::new(|| 42);
120 let second = BoxSupplier::new(|| "hello");
121 let zipped = first.zip(second);
122 println!("Zipped: {:?}", zipped.get());
123
124 // Memoize (FnMut)
125 let mut call_count = 0;
126 let mut memoized = BoxStatefulSupplier::new(move || {
127 call_count += 1;
128 println!(" Expensive computation #{}", call_count);
129 42
130 })
131 .memoize();
132 println!("First call: {}", memoized.get());
133 println!("Second call (cached): {}", memoized.get());
134 println!();
135}Trait Implementations§
Source§impl<T> Debug for BoxSupplier<T>
impl<T> Debug for BoxSupplier<T>
Source§impl<T> Display for BoxSupplier<T>
impl<T> Display for BoxSupplier<T>
Source§impl<T> Supplier<T> for BoxSupplier<T>
impl<T> Supplier<T> for BoxSupplier<T>
Source§fn into_box(self) -> BoxSupplier<T>
fn into_box(self) -> BoxSupplier<T>
BoxSupplier. Read moreSource§fn into_rc(self) -> RcSupplier<T>where
Self: 'static,
fn into_rc(self) -> RcSupplier<T>where
Self: 'static,
RcSupplier. Read moreSource§fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
BoxSupplierOnce. Read more