pub trait Supplier<T> {
// Required methods
fn get(&mut self) -> T;
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;
}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 selfto 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§
Sourcefn get(&mut self) -> T
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);