Supplier

Trait Supplier 

Source
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 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);
Source

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

Converts to BoxSupplier.

§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);
Source

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

Converts to RcSupplier.

§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);
Source

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

Converts to ArcSupplier.

§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);

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,