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
179 .lock()
180 .expect("mutex should not be poisoned");
181 *c += 1;
182 println!(" Computation #{}", *c);
183 42
184 });
185
186 // Memoization with ArcStatefulSupplier
187 let memoized = source.memoize();
188 let mut m = memoized;
189 println!("First call: {}", m.get());
190 println!("Second call (cached): {}", m.get());
191 println!(
192 "Call count: {}",
193 *call_count.lock().expect("mutex should not be poisoned")
194 );
195 println!();
196}
197
198fn demo_arc_supplier_threading() {
199 println!("--- ArcStatefulSupplier Threading ---");
200
201 let counter = Arc::new(Mutex::new(0));
202 let counter_clone = Arc::clone(&counter);
203
204 let supplier = ArcStatefulSupplier::new(move || {
205 let mut c = counter_clone.lock().expect("mutex should not be poisoned");
206 *c += 1;
207 *c
208 });
209
210 let mut s1 = supplier.clone();
211 let mut s2 = supplier.clone();
212 let mut s3 = supplier;
213
214 let h1 = thread::spawn(move || {
215 let v1 = s1.get();
216 let v2 = s1.get();
217 println!("Thread 1: {} {}", v1, v2);
218 (v1, v2)
219 });
220
221 let h2 = thread::spawn(move || {
222 let v1 = s2.get();
223 let v2 = s2.get();
224 println!("Thread 2: {} {}", v1, v2);
225 (v1, v2)
226 });
227
228 let v1 = s3.get();
229 let v2 = s3.get();
230 println!("Main thread: {} {}", v1, v2);
231
232 h1.join().expect("thread should not panic");
233 h2.join().expect("thread should not panic");
234
235 println!(
236 "Final counter: {}",
237 *counter.lock().expect("mutex should not be poisoned")
238 );
239 println!();
240}
241
242fn demo_rc_supplier() {
243 println!("--- RcSupplier ---");
244
245 // Basic usage (Fn)
246 let supplier = RcSupplier::new(|| 42);
247 let s = supplier;
248 println!("Basic: {}", s.get());
249
250 // Shared state (FnMut) - use RcStatefulSupplier
251 let counter = Rc::new(RefCell::new(0));
252 let counter_clone = Rc::clone(&counter);
253 let supplier = RcStatefulSupplier::new(move || {
254 let mut c = counter_clone.borrow_mut();
255 *c += 1;
256 *c
257 });
258
259 let mut s1 = supplier.clone();
260 let mut s2 = supplier.clone();
261
262 println!("First clone: {}", s1.get());
263 println!("Second clone: {}", s2.get());
264 println!("First clone again: {}", s1.get());
265
266 // Reusable transformations (Fn)
267 let source = RcSupplier::new(|| 10);
268 let doubled = source.map(|x| x * 2);
269 let tripled = source.map(|x| x * 3);
270 let squared = source.map(|x| x * x);
271
272 let s = source;
273 let d = doubled;
274 let t = tripled;
275 let sq = squared;
276
277 println!("Source: {}", s.get());
278 println!("Doubled: {}", d.get());
279 println!("Tripled: {}", t.get());
280 println!("Squared: {}", sq.get());
281 println!();
282}
283
284fn demo_type_conversions() {
285 println!("--- Type Conversions ---");
286
287 // Closure to Box (Fn)
288 let closure = || 42;
289 let boxed = Supplier::into_box(closure);
290 println!("Closure -> Box: {}", boxed.get());
291
292 // Closure to Rc (Fn)
293 let closure = || 100;
294 let rc = Supplier::into_rc(closure);
295 println!("Closure -> Rc: {}", rc.get());
296
297 // Closure to Arc (Fn)
298 let closure = || 200;
299 let arc = Supplier::into_arc(closure);
300 println!("Closure -> Arc: {}", arc.get());
301
302 // Box to Rc (Fn)
303 let boxed = BoxSupplier::new(|| 42);
304 let rc = boxed.into_rc();
305 println!("Box -> Rc: {}", rc.get());
306
307 // Arc to Box (Fn)
308 let arc = ArcSupplier::new(|| 42);
309 let boxed = arc.into_box();
310 println!("Arc -> Box: {}", boxed.get());
311
312 // Rc to Box (Fn)
313 let rc = RcSupplier::new(|| 42);
314 let boxed = rc.into_box();
315 println!("Rc -> Box: {}", boxed.get());
316
317 println!();
318}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