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);§Author
Haixing Hu
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?
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}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?
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}More examples
29fn main() {
30 println!("=== Closure StatefulSupplier Operations Demo ===\n");
31
32 // 1. FnMut closure using map
33 println!("1. FnMut closure using map:");
34 let mut counter = 0;
35 let mut mapped = (move || {
36 counter += 1;
37 counter
38 })
39 .map(|x| x * 2);
40
41 println!(" First call: {}", mapped.get());
42 println!(" Second call: {}\n", mapped.get());
43
44 // 2. FnMut closure using filter
45 println!("2. FnMut closure using filter:");
46 let mut counter2 = 0;
47 let mut filtered = (move || {
48 counter2 += 1;
49 counter2
50 })
51 .filter(is_even_i32);
52
53 println!(" First call (odd number): {:?}", filtered.get());
54 println!(" Second call (even number): {:?}\n", filtered.get());
55
56 // 3. FnMut closure using memoize
57 println!("3. FnMut closure using memoize:");
58 let mut call_count = 0;
59 let mut memoized = (move || {
60 call_count += 1;
61 println!(" Underlying function called {} times", call_count);
62 42
63 })
64 .memoize();
65
66 println!(" First call: {}", memoized.get());
67 println!(" Second call: {}", memoized.get());
68 println!(" Third call: {}\n", memoized.get());
69
70 // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
71 println!("4. Fn closure using map:");
72 let mut mapped_stateless = (|| 10).map(|x| x * 3).map(|x| x + 5);
73 println!(" Result: {}\n", mapped_stateless.get());
74
75 // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
76 println!("5. Fn closure using filter:");
77 let mut filtered_stateless = (|| 42).filter(is_even_i32);
78 println!(" Filtered even number: {:?}\n", filtered_stateless.get());
79
80 // 6. Chained operations
81 println!("6. Chained operations:");
82 let mut counter3 = 0;
83 let mut chained = (move || {
84 counter3 += 1;
85 counter3
86 })
87 .map(|x| x * 2)
88 .filter(greater_than_five)
89 .map(|opt: Option<i32>| opt.unwrap_or(0));
90
91 println!(" First call: {}", chained.get()); // 2, filtered out
92 println!(" Second call: {}", chained.get()); // 4, filtered out
93 println!(" Third call: {}", chained.get()); // 6, passed
94 println!(" Fourth call: {}\n", chained.get()); // 8, passed
95
96 println!("=== Demo completed ===");
97}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?
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}More examples
29fn main() {
30 println!("=== Closure StatefulSupplier Operations Demo ===\n");
31
32 // 1. FnMut closure using map
33 println!("1. FnMut closure using map:");
34 let mut counter = 0;
35 let mut mapped = (move || {
36 counter += 1;
37 counter
38 })
39 .map(|x| x * 2);
40
41 println!(" First call: {}", mapped.get());
42 println!(" Second call: {}\n", mapped.get());
43
44 // 2. FnMut closure using filter
45 println!("2. FnMut closure using filter:");
46 let mut counter2 = 0;
47 let mut filtered = (move || {
48 counter2 += 1;
49 counter2
50 })
51 .filter(is_even_i32);
52
53 println!(" First call (odd number): {:?}", filtered.get());
54 println!(" Second call (even number): {:?}\n", filtered.get());
55
56 // 3. FnMut closure using memoize
57 println!("3. FnMut closure using memoize:");
58 let mut call_count = 0;
59 let mut memoized = (move || {
60 call_count += 1;
61 println!(" Underlying function called {} times", call_count);
62 42
63 })
64 .memoize();
65
66 println!(" First call: {}", memoized.get());
67 println!(" Second call: {}", memoized.get());
68 println!(" Third call: {}\n", memoized.get());
69
70 // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
71 println!("4. Fn closure using map:");
72 let mut mapped_stateless = (|| 10).map(|x| x * 3).map(|x| x + 5);
73 println!(" Result: {}\n", mapped_stateless.get());
74
75 // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
76 println!("5. Fn closure using filter:");
77 let mut filtered_stateless = (|| 42).filter(is_even_i32);
78 println!(" Filtered even number: {:?}\n", filtered_stateless.get());
79
80 // 6. Chained operations
81 println!("6. Chained operations:");
82 let mut counter3 = 0;
83 let mut chained = (move || {
84 counter3 += 1;
85 counter3
86 })
87 .map(|x| x * 2)
88 .filter(greater_than_five)
89 .map(|opt: Option<i32>| opt.unwrap_or(0));
90
91 println!(" First call: {}", chained.get()); // 2, filtered out
92 println!(" Second call: {}", chained.get()); // 4, filtered out
93 println!(" Third call: {}", chained.get()); // 6, passed
94 println!(" Fourth call: {}\n", chained.get()); // 8, passed
95
96 println!("=== Demo completed ===");
97}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?
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 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