Supplier

Trait Supplier 

Source
pub trait Supplier<T> {
    // Required method
    fn get(&mut self) -> T;

    // Provided methods
    fn into_box(self) -> BoxSupplier<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_rc(self) -> RcSupplier<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_arc(self) -> ArcSupplier<T>
       where Self: Sized + Send + 'static,
             T: Send + 'static { ... }
    fn into_fn(self) -> impl FnMut() -> T
       where Self: Sized { ... }
    fn to_box(&self) -> BoxSupplier<T>
       where Self: Clone + Sized + 'static,
             T: 'static { ... }
    fn to_rc(&self) -> RcSupplier<T>
       where Self: Clone + Sized + 'static,
             T: 'static { ... }
    fn to_arc(&self) -> ArcSupplier<T>
       where Self: Clone + Sized + Send + 'static,
             T: Send + 'static { ... }
    fn to_fn(&self) -> impl FnMut() -> T
       where Self: Clone + Sized { ... }
}
Expand description

Supplier trait: generates and returns values without input.

The core abstraction for value generation. Similar to Java’s Supplier<T> interface, it produces values without taking any input parameters.

§Key Characteristics

  • No input parameters: Pure value generation
  • Mutable access: Uses &mut self to allow state changes
  • Returns ownership: Returns T (not &T) to avoid lifetime issues
  • Can modify state: Commonly used for counters, sequences, and generators

§Automatically Implemented for Closures

All FnMut() -> T closures automatically implement this trait, enabling seamless integration with both raw closures and wrapped supplier types.

§Examples

§Using with Generic Functions

use prism3_function::{Supplier, BoxSupplier};

fn call_twice<S: Supplier<i32>>(supplier: &mut S) -> (i32, i32) {
    (supplier.get(), supplier.get())
}

let mut s = BoxSupplier::new(|| 42);
assert_eq!(call_twice(&mut s), (42, 42));

let mut closure = || 100;
assert_eq!(call_twice(&mut closure), (100, 100));

§Stateful Supplier

use prism3_function::Supplier;

let mut counter = 0;
let mut stateful = || {
    counter += 1;
    counter
};

assert_eq!(stateful.get(), 1);
assert_eq!(stateful.get(), 2);

§Author

Haixing Hu

Required Methods§

Source

fn get(&mut self) -> T

Generates and returns the next value.

Executes the underlying function and returns the generated value. Uses &mut self because suppliers typically involve state changes (counters, sequences, etc.).

§Returns

The generated value of type T

§Examples
use prism3_function::{Supplier, BoxSupplier};

let mut supplier = BoxSupplier::new(|| 42);
assert_eq!(supplier.get(), 42);

Provided Methods§

Source

fn into_box(self) -> BoxSupplier<T>
where Self: Sized + 'static, T: 'static,

Converts to BoxSupplier.

This method has a default implementation that wraps the supplier in a BoxSupplier. Custom implementations can override this for more efficient conversions.

§Returns

A new BoxSupplier<T> instance

§Examples
use prism3_function::Supplier;

let closure = || 42;
let mut boxed = closure.into_box();
assert_eq!(boxed.get(), 42);
Examples found in repository?
examples/supplier_demo.rs (line 38)
33fn demo_closure_supplier() {
34    println!("--- Closure as Supplier ---");
35
36    // Simple closure
37    let closure = || 42;
38    let mut boxed = Supplier::into_box(closure);
39    println!("Closure: {}", boxed.get());
40
41    // Stateful closure
42    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    // 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}
Source

fn into_rc(self) -> RcSupplier<T>
where Self: Sized + 'static, T: 'static,

Converts to RcSupplier.

This method has a default implementation that wraps the supplier in an RcSupplier. Custom implementations can override this for more efficient conversions.

§Returns

A new RcSupplier<T> instance

§Examples
use prism3_function::Supplier;

let closure = || 42;
let mut rc = closure.into_rc();
assert_eq!(rc.get(), 42);
Examples found in repository?
examples/supplier_demo.rs (line 261)
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}
Source

fn into_arc(self) -> ArcSupplier<T>
where Self: Sized + Send + 'static, T: Send + 'static,

Converts to ArcSupplier.

This method has a default implementation that wraps the supplier in an ArcSupplier. Custom implementations can override this for more efficient conversions.

§Returns

A new ArcSupplier<T> instance

§Examples
use prism3_function::Supplier;

let closure = || 42;
let mut arc = closure.into_arc();
assert_eq!(arc.get(), 42);
Examples found in repository?
examples/supplier_demo.rs (line 266)
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}
Source

fn into_fn(self) -> impl FnMut() -> T
where Self: Sized,

Converts to a closure FnMut() -> T.

This method wraps the supplier in a closure that calls the get() method when invoked. This allows using suppliers in contexts that expect FnMut() closures.

§Returns

A closure impl FnMut() -> T

§Examples
use prism3_function::{Supplier, BoxSupplier};

let supplier = BoxSupplier::new(|| 42);
let mut closure = supplier.into_fn();
assert_eq!(closure(), 42);
assert_eq!(closure(), 42);
§Using with functions that expect FnMut
use prism3_function::{Supplier, BoxSupplier};

fn call_fn_twice<F: FnMut() -> i32>(mut f: F) -> (i32, i32) {
    (f(), f())
}

let supplier = BoxSupplier::new(|| 100);
let closure = supplier.into_fn();
assert_eq!(call_fn_twice(closure), (100, 100));
Source

fn to_box(&self) -> BoxSupplier<T>
where Self: Clone + Sized + 'static, T: 'static,

Creates a BoxSupplier from a cloned supplier.

Uses Clone to obtain an owned copy and converts it into a BoxSupplier. Implementations can override this for a more efficient conversion.

Source

fn to_rc(&self) -> RcSupplier<T>
where Self: Clone + Sized + 'static, T: 'static,

Creates an RcSupplier from a cloned supplier.

Uses Clone to obtain an owned copy and converts it into an RcSupplier. Implementations can override it for better performance.

Source

fn to_arc(&self) -> ArcSupplier<T>
where Self: Clone + Sized + Send + 'static, T: Send + 'static,

Creates an ArcSupplier from a cloned supplier.

Requires the supplier and produced values to be Send so the resulting supplier can be shared across threads.

Source

fn to_fn(&self) -> impl FnMut() -> T
where Self: Clone + Sized,

Creates a closure from a cloned supplier.

The default implementation clones self and consumes the clone to produce a closure. Concrete suppliers can override it to avoid the additional clone.

Implementors§

Source§

impl<T> Supplier<T> for ArcSupplier<T>

Source§

impl<T> Supplier<T> for BoxSupplier<T>

Source§

impl<T> Supplier<T> for RcSupplier<T>

Source§

impl<T, F> Supplier<T> for F
where F: FnMut() -> T,