pub struct BoxSupplier<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 prism3_function::{BoxSupplier, Supplier};
let supplier = BoxSupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here§Examples
§Counter
use prism3_function::{BoxSupplier, Supplier};
let mut counter = 0;
let mut supplier = BoxSupplier::new(move || {
counter += 1;
counter
});
assert_eq!(supplier.get(), 1);
assert_eq!(supplier.get(), 2);§Method Chaining
use prism3_function::{BoxSupplier, Supplier};
let mut 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>where
T: 'static,
impl<T> BoxSupplier<T>where
T: 'static,
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 BoxSupplier.
§Parameters
f- The closure to wrap
§Returns
A new BoxSupplier<T> instance
§Examples
use prism3_function::{BoxSupplier, Supplier};
let mut supplier = BoxSupplier::new(|| 42);
assert_eq!(supplier.get(), 42);Examples found in repository?
54fn demo_box_supplier_basics() {
55 println!("--- BoxSupplier Basics ---");
56
57 // Basic usage
58 let mut supplier = BoxSupplier::new(|| 42);
59 println!("Basic: {}", supplier.get());
60
61 // Constant supplier
62 let mut constant = BoxSupplier::constant(100);
63 println!("Constant: {}", constant.get());
64 println!("Constant: {}", constant.get());
65
66 // Stateful counter
67 let mut counter = 0;
68 let mut counter_supplier = BoxSupplier::new(move || {
69 counter += 1;
70 counter
71 });
72 println!("Counter: {}", counter_supplier.get());
73 println!("Counter: {}", counter_supplier.get());
74 println!("Counter: {}", counter_supplier.get());
75 println!();
76}
77
78fn demo_box_supplier_methods() {
79 println!("--- BoxSupplier Methods ---");
80
81 // Map
82 let mut mapped = BoxSupplier::new(|| 10).map(|x| x * 2).map(|x| x + 5);
83 println!("Mapped: {}", mapped.get());
84
85 // Filter
86 let mut counter = 0;
87 let mut filtered = BoxSupplier::new(move || {
88 counter += 1;
89 counter
90 })
91 .filter(|x| x % 2 == 0);
92 println!("Filtered (odd): {:?}", filtered.get());
93 println!("Filtered (even): {:?}", filtered.get());
94
95 // Zip
96 let first = BoxSupplier::new(|| 42);
97 let second = BoxSupplier::new(|| "hello");
98 let mut zipped = first.zip(second);
99 println!("Zipped: {:?}", zipped.get());
100
101 // Memoize
102 let mut call_count = 0;
103 let mut memoized = BoxSupplier::new(move || {
104 call_count += 1;
105 println!(" Expensive computation #{}", call_count);
106 42
107 })
108 .memoize();
109 println!("First call: {}", memoized.get());
110 println!("Second call (cached): {}", memoized.get());
111 println!();
112}
113
114fn demo_box_supplier_once() {
115 println!("--- BoxSupplierOnce ---");
116
117 // Basic usage
118 let once = BoxSupplierOnce::new(|| {
119 println!(" Expensive initialization");
120 42
121 });
122 println!("Value: {}", once.get());
123
124 // Moving captured values
125 let data = String::from("Hello, World!");
126 let once = BoxSupplierOnce::new(move || data);
127 println!("Moved data: {}", once.get());
128 println!();
129}
130
131fn demo_arc_supplier() {
132 println!("--- ArcSupplier ---");
133
134 // Basic usage
135 let supplier = ArcSupplier::new(|| 42);
136 let mut s = supplier;
137 println!("Basic: {}", s.get());
138
139 // Reusable transformations
140 let source = ArcSupplier::new(|| 10);
141 let doubled = source.map(|x| x * 2);
142 let tripled = source.map(|x| x * 3);
143
144 let mut s = source;
145 let mut d = doubled;
146 let mut t = tripled;
147 println!("Source: {}", s.get());
148 println!("Doubled: {}", d.get());
149 println!("Tripled: {}", t.get());
150
151 // Memoization
152 let call_count = Arc::new(Mutex::new(0));
153 let call_count_clone = Arc::clone(&call_count);
154 let source = ArcSupplier::new(move || {
155 let mut c = call_count_clone.lock().unwrap();
156 *c += 1;
157 println!(" Computation #{}", *c);
158 42
159 });
160 let memoized = source.memoize();
161
162 let mut m = memoized;
163 println!("First call: {}", m.get());
164 println!("Second call (cached): {}", m.get());
165 println!();
166}
167
168fn demo_arc_supplier_threading() {
169 println!("--- ArcSupplier Threading ---");
170
171 let counter = Arc::new(Mutex::new(0));
172 let counter_clone = Arc::clone(&counter);
173
174 let supplier = ArcSupplier::new(move || {
175 let mut c = counter_clone.lock().unwrap();
176 *c += 1;
177 *c
178 });
179
180 let mut s1 = supplier.clone();
181 let mut s2 = supplier.clone();
182 let mut s3 = supplier;
183
184 let h1 = thread::spawn(move || {
185 let v1 = s1.get();
186 let v2 = s1.get();
187 println!("Thread 1: {} {}", v1, v2);
188 (v1, v2)
189 });
190
191 let h2 = thread::spawn(move || {
192 let v1 = s2.get();
193 let v2 = s2.get();
194 println!("Thread 2: {} {}", v1, v2);
195 (v1, v2)
196 });
197
198 let v1 = s3.get();
199 let v2 = s3.get();
200 println!("Main thread: {} {}", v1, v2);
201
202 h1.join().unwrap();
203 h2.join().unwrap();
204
205 println!("Final counter: {}", *counter.lock().unwrap());
206 println!();
207}
208
209fn demo_rc_supplier() {
210 println!("--- RcSupplier ---");
211
212 // Basic usage
213 let supplier = RcSupplier::new(|| 42);
214 let mut s = supplier;
215 println!("Basic: {}", s.get());
216
217 // Shared state
218 let counter = Rc::new(RefCell::new(0));
219 let counter_clone = Rc::clone(&counter);
220 let supplier = RcSupplier::new(move || {
221 let mut c = counter_clone.borrow_mut();
222 *c += 1;
223 *c
224 });
225
226 let mut s1 = supplier.clone();
227 let mut s2 = supplier.clone();
228
229 println!("First clone: {}", s1.get());
230 println!("Second clone: {}", s2.get());
231 println!("First clone again: {}", s1.get());
232
233 // Reusable transformations
234 let source = RcSupplier::new(|| 10);
235 let doubled = source.map(|x| x * 2);
236 let tripled = source.map(|x| x * 3);
237 let squared = source.map(|x| x * x);
238
239 let mut s = source;
240 let mut d = doubled;
241 let mut t = tripled;
242 let mut sq = squared;
243
244 println!("Source: {}", s.get());
245 println!("Doubled: {}", d.get());
246 println!("Tripled: {}", t.get());
247 println!("Squared: {}", sq.get());
248 println!();
249}
250
251fn demo_type_conversions() {
252 println!("--- Type Conversions ---");
253
254 // Closure to Box
255 let closure = || 42;
256 let mut boxed = Supplier::into_box(closure);
257 println!("Closure -> Box: {}", boxed.get());
258
259 // Closure to Rc
260 let closure = || 100;
261 let mut rc = closure.into_rc();
262 println!("Closure -> Rc: {}", rc.get());
263
264 // Closure to Arc
265 let closure = || 200;
266 let mut arc = closure.into_arc();
267 println!("Closure -> Arc: {}", arc.get());
268
269 // Box to Rc
270 let boxed = BoxSupplier::new(|| 42);
271 let mut rc = boxed.into_rc();
272 println!("Box -> Rc: {}", rc.get());
273
274 // Arc to Box
275 let arc = ArcSupplier::new(|| 42);
276 let mut boxed = arc.into_box();
277 println!("Arc -> Box: {}", boxed.get());
278
279 // Rc to Box
280 let rc = RcSupplier::new(|| 42);
281 let mut boxed = rc.into_box();
282 println!("Rc -> Box: {}", boxed.get());
283
284 println!();
285}Sourcepub fn constant(value: T) -> Selfwhere
T: Clone + 'static,
pub fn constant(value: T) -> Selfwhere
T: Clone + 'static,
Creates a constant supplier.
Returns a supplier that always produces the same value (via cloning).
§Parameters
value- The constant value to return
§Returns
A constant supplier
§Examples
use prism3_function::{BoxSupplier, Supplier};
let mut constant = BoxSupplier::constant(42);
assert_eq!(constant.get(), 42);
assert_eq!(constant.get(), 42);Examples found in repository?
54fn demo_box_supplier_basics() {
55 println!("--- BoxSupplier Basics ---");
56
57 // Basic usage
58 let mut supplier = BoxSupplier::new(|| 42);
59 println!("Basic: {}", supplier.get());
60
61 // Constant supplier
62 let mut constant = BoxSupplier::constant(100);
63 println!("Constant: {}", constant.get());
64 println!("Constant: {}", constant.get());
65
66 // Stateful counter
67 let mut counter = 0;
68 let mut counter_supplier = BoxSupplier::new(move || {
69 counter += 1;
70 counter
71 });
72 println!("Counter: {}", counter_supplier.get());
73 println!("Counter: {}", counter_supplier.get());
74 println!("Counter: {}", counter_supplier.get());
75 println!();
76}Sourcepub fn map<U, F>(self, mapper: F) -> BoxSupplier<U>where
F: Mapper<T, U> + 'static,
U: 'static,
pub fn map<U, F>(self, mapper: F) -> BoxSupplier<U>where
F: Mapper<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 mapper to apply to the output. Can be:- A closure:
|x: T| -> U - A function pointer:
fn(T) -> U - A
BoxMapper<T, U>,RcMapper<T, U>,ArcMapper<T, U> - Any type implementing
Mapper<T, U>
- A closure:
§Returns
A new mapped BoxSupplier<U>
§Examples
§Using with closure
use prism3_function::{BoxSupplier, Supplier};
let mut mapped = BoxSupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(mapped.get(), 25);§Using with Mapper object
use prism3_function::{BoxSupplier, BoxMapper, Supplier, Mapper};
let mapper = BoxMapper::new(|x: i32| x * 2);
let mut supplier = BoxSupplier::new(|| 10)
.map(mapper);
assert_eq!(supplier.get(), 20);Examples found in repository?
78fn demo_box_supplier_methods() {
79 println!("--- BoxSupplier Methods ---");
80
81 // Map
82 let mut mapped = BoxSupplier::new(|| 10).map(|x| x * 2).map(|x| x + 5);
83 println!("Mapped: {}", mapped.get());
84
85 // Filter
86 let mut counter = 0;
87 let mut filtered = BoxSupplier::new(move || {
88 counter += 1;
89 counter
90 })
91 .filter(|x| x % 2 == 0);
92 println!("Filtered (odd): {:?}", filtered.get());
93 println!("Filtered (even): {:?}", filtered.get());
94
95 // Zip
96 let first = BoxSupplier::new(|| 42);
97 let second = BoxSupplier::new(|| "hello");
98 let mut zipped = first.zip(second);
99 println!("Zipped: {:?}", zipped.get());
100
101 // Memoize
102 let mut call_count = 0;
103 let mut memoized = BoxSupplier::new(move || {
104 call_count += 1;
105 println!(" Expensive computation #{}", call_count);
106 42
107 })
108 .memoize();
109 println!("First call: {}", memoized.get());
110 println!("Second call (cached): {}", memoized.get());
111 println!();
112}More examples
18fn main() {
19 println!("=== Closure Supplier Operations Demo ===\n");
20
21 // 1. FnMut closure using map
22 println!("1. FnMut closure using map:");
23 let mut counter = 0;
24 let mut mapped = (move || {
25 counter += 1;
26 counter
27 })
28 .map(|x| x * 2);
29
30 println!(" First call: {}", mapped.get());
31 println!(" Second call: {}\n", mapped.get());
32
33 // 2. FnMut closure using filter
34 println!("2. FnMut closure using filter:");
35 let mut counter2 = 0;
36 let mut filtered = (move || {
37 counter2 += 1;
38 counter2
39 })
40 .filter(|x| x % 2 == 0);
41
42 println!(" First call (odd number): {:?}", filtered.get());
43 println!(" Second call (even number): {:?}\n", filtered.get());
44
45 // 3. FnMut closure using memoize
46 println!("3. FnMut closure using memoize:");
47 let mut call_count = 0;
48 let mut memoized = (move || {
49 call_count += 1;
50 println!(" Underlying function called {} times", call_count);
51 42
52 })
53 .memoize();
54
55 println!(" First call: {}", memoized.get());
56 println!(" Second call: {}", memoized.get());
57 println!(" Third call: {}\n", memoized.get());
58
59 // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
60 println!("4. Fn closure using map:");
61 let mut mapped_readonly = (|| 10).map(|x| x * 3).map(|x| x + 5);
62 println!(" Result: {}\n", mapped_readonly.get());
63
64 // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
65 println!("5. Fn closure using filter:");
66 let mut filtered_readonly = (|| 42).filter(|x| x % 2 == 0);
67 println!(" Filtered even number: {:?}\n", filtered_readonly.get());
68
69 // 6. Chained operations
70 println!("6. Chained operations:");
71 let mut counter3 = 0;
72 let mut chained = (move || {
73 counter3 += 1;
74 counter3
75 })
76 .map(|x| x * 2)
77 .filter(|x| *x > 5)
78 .map(|opt: Option<i32>| opt.unwrap_or(0));
79
80 println!(" First call: {}", chained.get()); // 2, filtered out
81 println!(" Second call: {}", chained.get()); // 4, filtered out
82 println!(" Third call: {}", chained.get()); // 6, passed
83 println!(" Fourth call: {}\n", chained.get()); // 8, passed
84
85 println!("=== Demo completed ===");
86}Sourcepub fn filter<P>(self, predicate: P) -> BoxSupplier<Option<T>>
pub fn filter<P>(self, predicate: P) -> BoxSupplier<Option<T>>
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 BoxSupplier<Option<T>>
§Examples
use prism3_function::{BoxSupplier, Supplier};
let mut counter = 0;
let mut filtered = BoxSupplier::new(move || {
counter += 1;
counter
}).filter(|x| x % 2 == 0);
assert_eq!(filtered.get(), None); // 1 is odd
assert_eq!(filtered.get(), Some(2)); // 2 is evenExamples found in repository?
78fn demo_box_supplier_methods() {
79 println!("--- BoxSupplier Methods ---");
80
81 // Map
82 let mut mapped = BoxSupplier::new(|| 10).map(|x| x * 2).map(|x| x + 5);
83 println!("Mapped: {}", mapped.get());
84
85 // Filter
86 let mut counter = 0;
87 let mut filtered = BoxSupplier::new(move || {
88 counter += 1;
89 counter
90 })
91 .filter(|x| x % 2 == 0);
92 println!("Filtered (odd): {:?}", filtered.get());
93 println!("Filtered (even): {:?}", filtered.get());
94
95 // Zip
96 let first = BoxSupplier::new(|| 42);
97 let second = BoxSupplier::new(|| "hello");
98 let mut zipped = first.zip(second);
99 println!("Zipped: {:?}", zipped.get());
100
101 // Memoize
102 let mut call_count = 0;
103 let mut memoized = BoxSupplier::new(move || {
104 call_count += 1;
105 println!(" Expensive computation #{}", call_count);
106 42
107 })
108 .memoize();
109 println!("First call: {}", memoized.get());
110 println!("Second call (cached): {}", memoized.get());
111 println!();
112}More examples
18fn main() {
19 println!("=== Closure Supplier Operations Demo ===\n");
20
21 // 1. FnMut closure using map
22 println!("1. FnMut closure using map:");
23 let mut counter = 0;
24 let mut mapped = (move || {
25 counter += 1;
26 counter
27 })
28 .map(|x| x * 2);
29
30 println!(" First call: {}", mapped.get());
31 println!(" Second call: {}\n", mapped.get());
32
33 // 2. FnMut closure using filter
34 println!("2. FnMut closure using filter:");
35 let mut counter2 = 0;
36 let mut filtered = (move || {
37 counter2 += 1;
38 counter2
39 })
40 .filter(|x| x % 2 == 0);
41
42 println!(" First call (odd number): {:?}", filtered.get());
43 println!(" Second call (even number): {:?}\n", filtered.get());
44
45 // 3. FnMut closure using memoize
46 println!("3. FnMut closure using memoize:");
47 let mut call_count = 0;
48 let mut memoized = (move || {
49 call_count += 1;
50 println!(" Underlying function called {} times", call_count);
51 42
52 })
53 .memoize();
54
55 println!(" First call: {}", memoized.get());
56 println!(" Second call: {}", memoized.get());
57 println!(" Third call: {}\n", memoized.get());
58
59 // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
60 println!("4. Fn closure using map:");
61 let mut mapped_readonly = (|| 10).map(|x| x * 3).map(|x| x + 5);
62 println!(" Result: {}\n", mapped_readonly.get());
63
64 // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
65 println!("5. Fn closure using filter:");
66 let mut filtered_readonly = (|| 42).filter(|x| x % 2 == 0);
67 println!(" Filtered even number: {:?}\n", filtered_readonly.get());
68
69 // 6. Chained operations
70 println!("6. Chained operations:");
71 let mut counter3 = 0;
72 let mut chained = (move || {
73 counter3 += 1;
74 counter3
75 })
76 .map(|x| x * 2)
77 .filter(|x| *x > 5)
78 .map(|opt: Option<i32>| opt.unwrap_or(0));
79
80 println!(" First call: {}", chained.get()); // 2, filtered out
81 println!(" Second call: {}", chained.get()); // 4, filtered out
82 println!(" Third call: {}", chained.get()); // 6, passed
83 println!(" Fourth call: {}\n", chained.get()); // 8, passed
84
85 println!("=== Demo completed ===");
86}Sourcepub fn zip<U>(self, other: BoxSupplier<U>) -> BoxSupplier<(T, U)>where
U: 'static,
pub fn zip<U>(self, other: BoxSupplier<U>) -> BoxSupplier<(T, U)>where
U: 'static,
Combines this supplier with another, producing a tuple.
Consumes both suppliers and returns a new supplier that
produces (T, U) tuples.
§Parameters
other- The other supplier to combine with
§Returns
A new BoxSupplier<(T, U)>
§Examples
use prism3_function::{BoxSupplier, Supplier};
let first = BoxSupplier::new(|| 42);
let second = BoxSupplier::new(|| "hello");
let mut zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));Examples found in repository?
78fn demo_box_supplier_methods() {
79 println!("--- BoxSupplier Methods ---");
80
81 // Map
82 let mut mapped = BoxSupplier::new(|| 10).map(|x| x * 2).map(|x| x + 5);
83 println!("Mapped: {}", mapped.get());
84
85 // Filter
86 let mut counter = 0;
87 let mut filtered = BoxSupplier::new(move || {
88 counter += 1;
89 counter
90 })
91 .filter(|x| x % 2 == 0);
92 println!("Filtered (odd): {:?}", filtered.get());
93 println!("Filtered (even): {:?}", filtered.get());
94
95 // Zip
96 let first = BoxSupplier::new(|| 42);
97 let second = BoxSupplier::new(|| "hello");
98 let mut zipped = first.zip(second);
99 println!("Zipped: {:?}", zipped.get());
100
101 // Memoize
102 let mut call_count = 0;
103 let mut memoized = BoxSupplier::new(move || {
104 call_count += 1;
105 println!(" Expensive computation #{}", call_count);
106 42
107 })
108 .memoize();
109 println!("First call: {}", memoized.get());
110 println!("Second call (cached): {}", memoized.get());
111 println!();
112}Sourcepub fn memoize(self) -> BoxSupplier<T>where
T: Clone + 'static,
pub fn memoize(self) -> BoxSupplier<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 BoxSupplier<T>
§Examples
use prism3_function::{BoxSupplier, Supplier};
let mut call_count = 0;
let mut memoized = BoxSupplier::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?
78fn demo_box_supplier_methods() {
79 println!("--- BoxSupplier Methods ---");
80
81 // Map
82 let mut mapped = BoxSupplier::new(|| 10).map(|x| x * 2).map(|x| x + 5);
83 println!("Mapped: {}", mapped.get());
84
85 // Filter
86 let mut counter = 0;
87 let mut filtered = BoxSupplier::new(move || {
88 counter += 1;
89 counter
90 })
91 .filter(|x| x % 2 == 0);
92 println!("Filtered (odd): {:?}", filtered.get());
93 println!("Filtered (even): {:?}", filtered.get());
94
95 // Zip
96 let first = BoxSupplier::new(|| 42);
97 let second = BoxSupplier::new(|| "hello");
98 let mut zipped = first.zip(second);
99 println!("Zipped: {:?}", zipped.get());
100
101 // Memoize
102 let mut call_count = 0;
103 let mut memoized = BoxSupplier::new(move || {
104 call_count += 1;
105 println!(" Expensive computation #{}", call_count);
106 42
107 })
108 .memoize();
109 println!("First call: {}", memoized.get());
110 println!("Second call (cached): {}", memoized.get());
111 println!();
112}Trait Implementations§
Source§impl<T> Supplier<T> for BoxSupplier<T>
impl<T> Supplier<T> for BoxSupplier<T>
Source§fn into_box(self) -> BoxSupplier<T>where
T: 'static,
fn into_box(self) -> BoxSupplier<T>where
T: 'static,
BoxSupplier. Read moreSource§fn into_rc(self) -> RcSupplier<T>where
T: 'static,
fn into_rc(self) -> RcSupplier<T>where
T: 'static,
RcSupplier. Read more