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);§Author
Haixing Hu
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?
70fn demo_box_supplier_basics() {
71 println!("--- BoxSupplier Basics ---");
72
73 // Basic usage (Fn)
74 let supplier = BoxSupplier::new(|| 42);
75 println!("Basic: {}", supplier.get());
76
77 // Constant supplier (Fn)
78 let constant = BoxSupplier::constant(100);
79 println!("Constant: {}", constant.get());
80 println!("Constant: {}", constant.get());
81
82 // Stateful counter (FnMut) - use BoxStatefulSupplier
83 let mut counter = 0;
84 let mut counter_supplier = BoxStatefulSupplier::new(move || {
85 counter += 1;
86 counter
87 });
88 println!("Counter: {}", counter_supplier.get());
89 println!("Counter: {}", counter_supplier.get());
90 println!("Counter: {}", counter_supplier.get());
91 println!();
92}
93
94fn demo_box_supplier_methods() {
95 println!("--- BoxStatefulSupplier Methods ---");
96
97 // Map (FnMut)
98 let mut counter = 0;
99 let mut mapped = BoxStatefulSupplier::new(move || {
100 counter += 1;
101 counter * 10
102 })
103 .map(|x| x + 5);
104 println!("Mapped: {}", mapped.get());
105 println!("Mapped: {}", mapped.get());
106
107 // Filter (FnMut)
108 let mut counter = 0;
109 let mut filtered = BoxStatefulSupplier::new(move || {
110 counter += 1;
111 counter
112 })
113 .filter(is_even_i32);
114 println!("Filtered (odd): {:?}", filtered.get());
115 println!("Filtered (even): {:?}", filtered.get());
116
117 // Zip (Fn)
118 let first = BoxSupplier::new(|| 42);
119 let second = BoxSupplier::new(|| "hello");
120 let zipped = first.zip(second);
121 println!("Zipped: {:?}", zipped.get());
122
123 // Memoize (FnMut)
124 let mut call_count = 0;
125 let mut memoized = BoxStatefulSupplier::new(move || {
126 call_count += 1;
127 println!(" Expensive computation #{}", call_count);
128 42
129 })
130 .memoize();
131 println!("First call: {}", memoized.get());
132 println!("Second call (cached): {}", memoized.get());
133 println!();
134}
135
136fn demo_box_supplier_once() {
137 println!("--- BoxSupplierOnce ---");
138
139 // Basic usage
140 let once = BoxSupplierOnce::new(|| {
141 println!(" Expensive initialization");
142 42
143 });
144 println!("Value: {}", once.get());
145
146 // Moving captured values
147 let data = String::from("Hello, World!");
148 let once = BoxSupplierOnce::new(move || data);
149 println!("Moved data: {}", once.get());
150 println!();
151}
152
153fn demo_arc_supplier() {
154 println!("--- ArcSupplier ---");
155
156 // Basic usage (Fn)
157 let supplier = ArcSupplier::new(|| 42);
158 let s = supplier;
159 println!("Basic: {}", s.get());
160
161 // Reusable transformations (Fn)
162 let source = ArcSupplier::new(|| 10);
163 let doubled = source.map(|x| x * 2);
164 let tripled = source.map(|x| x * 3);
165
166 let s = source;
167 let d = doubled;
168 let t = tripled;
169 println!("Source: {}", s.get());
170 println!("Doubled: {}", d.get());
171 println!("Tripled: {}", t.get());
172
173 // Stateful with ArcStatefulSupplier (FnMut)
174 let call_count = Arc::new(Mutex::new(0));
175 let call_count_clone = Arc::clone(&call_count);
176 let source = ArcStatefulSupplier::new(move || {
177 let mut c = call_count_clone.lock().unwrap();
178 *c += 1;
179 println!(" Computation #{}", *c);
180 42
181 });
182
183 // Memoization with ArcStatefulSupplier
184 let memoized = source.memoize();
185 let mut m = memoized;
186 println!("First call: {}", m.get());
187 println!("Second call (cached): {}", m.get());
188 println!("Call count: {}", *call_count.lock().unwrap());
189 println!();
190}
191
192fn demo_arc_supplier_threading() {
193 println!("--- ArcStatefulSupplier Threading ---");
194
195 let counter = Arc::new(Mutex::new(0));
196 let counter_clone = Arc::clone(&counter);
197
198 let supplier = ArcStatefulSupplier::new(move || {
199 let mut c = counter_clone.lock().unwrap();
200 *c += 1;
201 *c
202 });
203
204 let mut s1 = supplier.clone();
205 let mut s2 = supplier.clone();
206 let mut s3 = supplier;
207
208 let h1 = thread::spawn(move || {
209 let v1 = s1.get();
210 let v2 = s1.get();
211 println!("Thread 1: {} {}", v1, v2);
212 (v1, v2)
213 });
214
215 let h2 = thread::spawn(move || {
216 let v1 = s2.get();
217 let v2 = s2.get();
218 println!("Thread 2: {} {}", v1, v2);
219 (v1, v2)
220 });
221
222 let v1 = s3.get();
223 let v2 = s3.get();
224 println!("Main thread: {} {}", v1, v2);
225
226 h1.join().unwrap();
227 h2.join().unwrap();
228
229 println!("Final counter: {}", *counter.lock().unwrap());
230 println!();
231}
232
233fn demo_rc_supplier() {
234 println!("--- RcSupplier ---");
235
236 // Basic usage (Fn)
237 let supplier = RcSupplier::new(|| 42);
238 let s = supplier;
239 println!("Basic: {}", s.get());
240
241 // Shared state (FnMut) - use RcStatefulSupplier
242 let counter = Rc::new(RefCell::new(0));
243 let counter_clone = Rc::clone(&counter);
244 let supplier = RcStatefulSupplier::new(move || {
245 let mut c = counter_clone.borrow_mut();
246 *c += 1;
247 *c
248 });
249
250 let mut s1 = supplier.clone();
251 let mut s2 = supplier.clone();
252
253 println!("First clone: {}", s1.get());
254 println!("Second clone: {}", s2.get());
255 println!("First clone again: {}", s1.get());
256
257 // Reusable transformations (Fn)
258 let source = RcSupplier::new(|| 10);
259 let doubled = source.map(|x| x * 2);
260 let tripled = source.map(|x| x * 3);
261 let squared = source.map(|x| x * x);
262
263 let s = source;
264 let d = doubled;
265 let t = tripled;
266 let sq = squared;
267
268 println!("Source: {}", s.get());
269 println!("Doubled: {}", d.get());
270 println!("Tripled: {}", t.get());
271 println!("Squared: {}", sq.get());
272 println!();
273}
274
275fn demo_type_conversions() {
276 println!("--- Type Conversions ---");
277
278 // Closure to Box (Fn)
279 let closure = || 42;
280 let boxed = Supplier::into_box(closure);
281 println!("Closure -> Box: {}", boxed.get());
282
283 // Closure to Rc (Fn)
284 let closure = || 100;
285 let rc = Supplier::into_rc(closure);
286 println!("Closure -> Rc: {}", rc.get());
287
288 // Closure to Arc (Fn)
289 let closure = || 200;
290 let arc = Supplier::into_arc(closure);
291 println!("Closure -> Arc: {}", arc.get());
292
293 // Box to Rc (Fn)
294 let boxed = BoxSupplier::new(|| 42);
295 let rc = boxed.into_rc();
296 println!("Box -> Rc: {}", rc.get());
297
298 // Arc to Box (Fn)
299 let arc = ArcSupplier::new(|| 42);
300 let boxed = arc.into_box();
301 println!("Arc -> Box: {}", boxed.get());
302
303 // Rc to Box (Fn)
304 let rc = RcSupplier::new(|| 42);
305 let boxed = rc.into_box();
306 println!("Rc -> Box: {}", boxed.get());
307
308 println!();
309}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?
70fn demo_box_supplier_basics() {
71 println!("--- BoxSupplier Basics ---");
72
73 // Basic usage (Fn)
74 let supplier = BoxSupplier::new(|| 42);
75 println!("Basic: {}", supplier.get());
76
77 // Constant supplier (Fn)
78 let constant = BoxSupplier::constant(100);
79 println!("Constant: {}", constant.get());
80 println!("Constant: {}", constant.get());
81
82 // Stateful counter (FnMut) - use BoxStatefulSupplier
83 let mut counter = 0;
84 let mut counter_supplier = BoxStatefulSupplier::new(move || {
85 counter += 1;
86 counter
87 });
88 println!("Counter: {}", counter_supplier.get());
89 println!("Counter: {}", counter_supplier.get());
90 println!("Counter: {}", counter_supplier.get());
91 println!();
92}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?
94fn demo_box_supplier_methods() {
95 println!("--- BoxStatefulSupplier Methods ---");
96
97 // Map (FnMut)
98 let mut counter = 0;
99 let mut mapped = BoxStatefulSupplier::new(move || {
100 counter += 1;
101 counter * 10
102 })
103 .map(|x| x + 5);
104 println!("Mapped: {}", mapped.get());
105 println!("Mapped: {}", mapped.get());
106
107 // Filter (FnMut)
108 let mut counter = 0;
109 let mut filtered = BoxStatefulSupplier::new(move || {
110 counter += 1;
111 counter
112 })
113 .filter(is_even_i32);
114 println!("Filtered (odd): {:?}", filtered.get());
115 println!("Filtered (even): {:?}", filtered.get());
116
117 // Zip (Fn)
118 let first = BoxSupplier::new(|| 42);
119 let second = BoxSupplier::new(|| "hello");
120 let zipped = first.zip(second);
121 println!("Zipped: {:?}", zipped.get());
122
123 // Memoize (FnMut)
124 let mut call_count = 0;
125 let mut memoized = BoxStatefulSupplier::new(move || {
126 call_count += 1;
127 println!(" Expensive computation #{}", call_count);
128 42
129 })
130 .memoize();
131 println!("First call: {}", memoized.get());
132 println!("Second call (cached): {}", memoized.get());
133 println!();
134}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