pub struct BoxStatefulSupplier<T> { /* private fields */ }Expand description
Box-based single ownership supplier.
Uses Box<dyn FnMut() -> T> for single ownership scenarios.
This is the most lightweight supplier with zero reference
counting overhead.
§Ownership Model
Methods consume self (move semantics). When you call a method
like map(), the original supplier is consumed and you get a new
one:
use qubit_function::{BoxStatefulSupplier, StatefulSupplier};
let supplier = BoxStatefulSupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here§Examples
§Counter
use qubit_function::{BoxStatefulSupplier, StatefulSupplier};
let mut counter = 0;
let mut supplier = BoxStatefulSupplier::new(move || {
counter += 1;
counter
});
assert_eq!(supplier.get(), 1);
assert_eq!(supplier.get(), 2);§Method Chaining
use qubit_function::{BoxStatefulSupplier, StatefulSupplier};
let mut pipeline = BoxStatefulSupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(pipeline.get(), 25);Implementations§
Source§impl<T> BoxStatefulSupplier<T>
impl<T> BoxStatefulSupplier<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> 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}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut() -> 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: FnMut() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnMut() -> 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 map<U, M>(self, mapper: M) -> BoxStatefulSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(self, mapper: M) -> BoxStatefulSupplier<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);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}More examples
30fn main() {
31 println!("=== Closure StatefulSupplier Operations Demo ===\n");
32
33 // 1. FnMut closure using map
34 println!("1. FnMut closure using map:");
35 let mut counter = 0;
36 let mut mapped = (move || {
37 counter += 1;
38 counter
39 })
40 .map(|x| x * 2);
41
42 println!(" First call: {}", mapped.get());
43 println!(" Second call: {}\n", mapped.get());
44
45 // 2. FnMut closure using filter
46 println!("2. FnMut closure using filter:");
47 let mut counter2 = 0;
48 let mut filtered = (move || {
49 counter2 += 1;
50 counter2
51 })
52 .filter(is_even_i32);
53
54 println!(" First call (odd number): {:?}", filtered.get());
55 println!(" Second call (even number): {:?}\n", filtered.get());
56
57 // 3. FnMut closure using memoize
58 println!("3. FnMut closure using memoize:");
59 let mut call_count = 0;
60 let mut memoized = (move || {
61 call_count += 1;
62 println!(" Underlying function called {} times", call_count);
63 42
64 })
65 .memoize();
66
67 println!(" First call: {}", memoized.get());
68 println!(" Second call: {}", memoized.get());
69 println!(" Third call: {}\n", memoized.get());
70
71 // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
72 println!("4. Fn closure using map:");
73 let mut mapped_stateless = (|| 10).map(|x| x * 3).map(|x| x + 5);
74 println!(" Result: {}\n", mapped_stateless.get());
75
76 // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
77 println!("5. Fn closure using filter:");
78 let mut filtered_stateless = (|| 42).filter(is_even_i32);
79 println!(" Filtered even number: {:?}\n", filtered_stateless.get());
80
81 // 6. Chained operations
82 println!("6. Chained operations:");
83 let mut counter3 = 0;
84 let mut chained = (move || {
85 counter3 += 1;
86 counter3
87 })
88 .map(|x| x * 2)
89 .filter(greater_than_five)
90 .map(|opt: Option<i32>| opt.unwrap_or(0));
91
92 println!(" First call: {}", chained.get()); // 2, filtered out
93 println!(" Second call: {}", chained.get()); // 4, filtered out
94 println!(" Third call: {}", chained.get()); // 6, passed
95 println!(" Fourth call: {}\n", chained.get()); // 8, passed
96
97 println!("=== Demo completed ===");
98}Sourcepub fn filter<P>(self, predicate: P) -> BoxStatefulSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(self, predicate: P) -> BoxStatefulSupplier<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));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}More examples
30fn main() {
31 println!("=== Closure StatefulSupplier Operations Demo ===\n");
32
33 // 1. FnMut closure using map
34 println!("1. FnMut closure using map:");
35 let mut counter = 0;
36 let mut mapped = (move || {
37 counter += 1;
38 counter
39 })
40 .map(|x| x * 2);
41
42 println!(" First call: {}", mapped.get());
43 println!(" Second call: {}\n", mapped.get());
44
45 // 2. FnMut closure using filter
46 println!("2. FnMut closure using filter:");
47 let mut counter2 = 0;
48 let mut filtered = (move || {
49 counter2 += 1;
50 counter2
51 })
52 .filter(is_even_i32);
53
54 println!(" First call (odd number): {:?}", filtered.get());
55 println!(" Second call (even number): {:?}\n", filtered.get());
56
57 // 3. FnMut closure using memoize
58 println!("3. FnMut closure using memoize:");
59 let mut call_count = 0;
60 let mut memoized = (move || {
61 call_count += 1;
62 println!(" Underlying function called {} times", call_count);
63 42
64 })
65 .memoize();
66
67 println!(" First call: {}", memoized.get());
68 println!(" Second call: {}", memoized.get());
69 println!(" Third call: {}\n", memoized.get());
70
71 // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
72 println!("4. Fn closure using map:");
73 let mut mapped_stateless = (|| 10).map(|x| x * 3).map(|x| x + 5);
74 println!(" Result: {}\n", mapped_stateless.get());
75
76 // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
77 println!("5. Fn closure using filter:");
78 let mut filtered_stateless = (|| 42).filter(is_even_i32);
79 println!(" Filtered even number: {:?}\n", filtered_stateless.get());
80
81 // 6. Chained operations
82 println!("6. Chained operations:");
83 let mut counter3 = 0;
84 let mut chained = (move || {
85 counter3 += 1;
86 counter3
87 })
88 .map(|x| x * 2)
89 .filter(greater_than_five)
90 .map(|opt: Option<i32>| opt.unwrap_or(0));
91
92 println!(" First call: {}", chained.get()); // 2, filtered out
93 println!(" Second call: {}", chained.get()); // 4, filtered out
94 println!(" Third call: {}", chained.get()); // 6, passed
95 println!(" Fourth call: {}\n", chained.get()); // 8, passed
96
97 println!("=== Demo completed ===");
98}Sourcepub fn zip<U, S>(self, other: S) -> BoxStatefulSupplier<(T, U)>where
T: 'static,
S: StatefulSupplier<U> + 'static,
U: 'static,
pub fn zip<U, S>(self, other: S) -> BoxStatefulSupplier<(T, U)>where
T: 'static,
S: StatefulSupplier<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"));Sourcepub fn memoize(self) -> BoxStatefulSupplier<T>where
T: Clone + 'static,
pub fn memoize(self) -> BoxStatefulSupplier<T>where
T: Clone + 'static,
Creates a memoizing supplier.
Returns a new supplier that caches the first value it produces. All subsequent calls return the cached value.
§Returns
A new memoized BoxStatefulSupplier<T>
§Examples
use qubit_function::{BoxStatefulSupplier, StatefulSupplier};
let mut call_count = 0;
let mut memoized = BoxStatefulSupplier::new(move || {
call_count += 1;
42
}).memoize();
assert_eq!(memoized.get(), 42); // Calls underlying function
assert_eq!(memoized.get(), 42); // Returns cached valueExamples 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 BoxStatefulSupplier<T>
impl<T> Debug for BoxStatefulSupplier<T>
Source§impl<T> Display for BoxStatefulSupplier<T>
impl<T> Display for BoxStatefulSupplier<T>
Source§impl<T> StatefulSupplier<T> for BoxStatefulSupplier<T>
impl<T> StatefulSupplier<T> for BoxStatefulSupplier<T>
Source§fn into_box(self) -> BoxStatefulSupplier<T>
fn into_box(self) -> BoxStatefulSupplier<T>
BoxStatefulSupplier. Read moreSource§fn into_rc(self) -> RcStatefulSupplier<T>where
Self: 'static,
fn into_rc(self) -> RcStatefulSupplier<T>where
Self: 'static,
RcStatefulSupplier. Read moreSource§fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
BoxSupplierOnce. Read more