supplier_demo/
supplier_demo.rs1use prism3_function::{
13 ArcSupplier, BoxSupplier, BoxSupplierOnce, RcSupplier, Supplier, SupplierOnce,
14};
15use std::cell::RefCell;
16use std::rc::Rc;
17use std::sync::{Arc, Mutex};
18use std::thread;
19
20fn main() {
21 println!("=== Supplier Demo ===\n");
22
23 demo_closure_supplier();
24 demo_box_supplier_basics();
25 demo_box_supplier_methods();
26 demo_box_supplier_once();
27 demo_arc_supplier();
28 demo_arc_supplier_threading();
29 demo_rc_supplier();
30 demo_type_conversions();
31}
32
33fn demo_closure_supplier() {
34 println!("--- Closure as Supplier ---");
35
36 let closure = || 42;
38 let mut boxed = Supplier::into_box(closure);
39 println!("Closure: {}", boxed.get());
40
41 let mut counter = 0;
43 let stateful = move || {
44 counter += 1;
45 counter
46 };
47 let mut boxed_stateful = Supplier::into_box(stateful);
48 println!("Counter: {}", boxed_stateful.get());
49 println!("Counter: {}", boxed_stateful.get());
50 println!("Counter: {}", boxed_stateful.get());
51 println!();
52}
53
54fn demo_box_supplier_basics() {
55 println!("--- BoxSupplier Basics ---");
56
57 let mut supplier = BoxSupplier::new(|| 42);
59 println!("Basic: {}", supplier.get());
60
61 let mut constant = BoxSupplier::constant(100);
63 println!("Constant: {}", constant.get());
64 println!("Constant: {}", constant.get());
65
66 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 let mut mapped = BoxSupplier::new(|| 10).map(|x| x * 2).map(|x| x + 5);
83 println!("Mapped: {}", mapped.get());
84
85 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 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 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 let once = BoxSupplierOnce::new(|| {
119 println!(" Expensive initialization");
120 42
121 });
122 println!("Value: {}", once.get());
123
124 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 let supplier = ArcSupplier::new(|| 42);
136 let mut s = supplier;
137 println!("Basic: {}", s.get());
138
139 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 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 let supplier = RcSupplier::new(|| 42);
214 let mut s = supplier;
215 println!("Basic: {}", s.get());
216
217 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 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 let closure = || 42;
256 let mut boxed = Supplier::into_box(closure);
257 println!("Closure -> Box: {}", boxed.get());
258
259 let closure = || 100;
261 let mut rc = closure.into_rc();
262 println!("Closure -> Rc: {}", rc.get());
263
264 let closure = || 200;
266 let mut arc = closure.into_arc();
267 println!("Closure -> Arc: {}", arc.get());
268
269 let boxed = BoxSupplier::new(|| 42);
271 let mut rc = boxed.into_rc();
272 println!("Box -> Rc: {}", rc.get());
273
274 let arc = ArcSupplier::new(|| 42);
276 let mut boxed = arc.into_box();
277 println!("Arc -> Box: {}", boxed.get());
278
279 let rc = RcSupplier::new(|| 42);
281 let mut boxed = rc.into_box();
282 println!("Rc -> Box: {}", boxed.get());
283
284 println!();
285}