Skip to main content

Supplier

Trait Supplier 

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

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

Read-only supplier trait: generates values without modifying state.

The core abstraction for stateless value generation. Unlike Supplier<T>, it uses &self instead of &mut self, enabling usage in read-only contexts and lock-free concurrent access.

§Key Characteristics

  • No input parameters: Pure value generation
  • Read-only access: Uses &self, doesn’t modify state
  • Returns ownership: Returns T (not &T) to avoid lifetime issues
  • Lock-free concurrency: Arc implementation doesn’t need Mutex

§Automatically Implemented for Closures

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

§Examples

§Using with Generic Functions

use qubit_function::{Supplier, BoxSupplier};

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

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

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

§Stateless Factory

use qubit_function::Supplier;

struct User {
    name: String,
}

impl User {
    fn new() -> Self {
        User {
            name: String::from("Default"),
        }
    }
}

let factory = || User::new();
let user1 = factory.get();
let user2 = factory.get();
// Each call creates a new User instance

§Author

Haixing Hu

Required Methods§

Source

fn get(&self) -> T

Generates and returns a value.

Executes the underlying function and returns the generated value. Uses &self because the supplier doesn’t modify its own state.

§Returns

The generated value of type T

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

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

Provided Methods§

Source

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

Converts to BoxSupplier.

This method has a default implementation that wraps the supplier in a BoxSupplier. Custom implementations can override this method for optimization purposes.

§Returns

A new BoxSupplier<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let boxed = closure.into_box();
assert_eq!(boxed.get(), 42);
Source

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

Converts to RcSupplier.

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

§Returns

A new RcSupplier<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let rc = closure.into_rc();
assert_eq!(rc.get(), 42);
Source

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

Converts to ArcSupplier.

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

§Returns

A new ArcSupplier<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let arc = closure.into_arc();
assert_eq!(arc.get(), 42);
Source

fn into_fn(self) -> impl Fn() -> T
where Self: Sized + 'static,

Converts to a closure implementing Fn() -> T.

This method has a default implementation that wraps the supplier in a closure. Custom implementations can override this method for optimization purposes.

§Returns

A closure implementing Fn() -> T

§Examples
use qubit_function::Supplier;

let closure = || 42;
let fn_closure = closure.into_fn();
assert_eq!(fn_closure(), 42);
assert_eq!(fn_closure(), 42);
Source

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

Converts to BoxSupplierOnce.

This method has a default implementation that wraps the supplier in a BoxSupplierOnce. Custom implementations can override this method for optimization purposes.

§Returns

A new BoxSupplierOnce<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let once = closure.into_once();
assert_eq!(once.get(), 42);
Source

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

Converts to BoxSupplier by cloning.

This method clones the supplier and wraps it in a BoxSupplier. Requires Self: Clone. Custom implementations can override this method for optimization.

§Returns

A new BoxSupplier<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let boxed = closure.to_box();
assert_eq!(boxed.get(), 42);
Source

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

Converts to RcSupplier by cloning.

This method clones the supplier and wraps it in an RcSupplier. Requires Self: Clone. Custom implementations can override this method for optimization.

§Returns

A new RcSupplier<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let rc = closure.to_rc();
assert_eq!(rc.get(), 42);
Source

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

Converts to ArcSupplier by cloning.

This method clones the supplier and wraps it in an ArcSupplier. Requires Self: Clone + Send + Sync. Custom implementations can override this method for optimization.

§Returns

A new ArcSupplier<T> instance

§Examples
use qubit_function::Supplier;

let closure = || 42;
let arc = closure.to_arc();
assert_eq!(arc.get(), 42);
Source

fn to_fn(&self) -> impl Fn() -> T
where Self: Clone + 'static,

Converts to a closure by cloning.

This method clones the supplier and wraps it in a closure implementing Fn() -> T. Requires Self: Clone. Custom implementations can override this method for optimization.

§Returns

A closure implementing Fn() -> T

§Examples
use qubit_function::Supplier;

let closure = || 42;
let fn_closure = closure.to_fn();
assert_eq!(fn_closure(), 42);
assert_eq!(fn_closure(), 42);
Source

fn to_once(&self) -> BoxSupplierOnce<T>
where Self: Clone + 'static,

Converts to BoxSupplierOnce without consuming self

⚠️ Requires Clone: This method requires Self to implement Clone. Clones the current supplier and converts the clone to a one-time supplier.

§Returns

Returns a BoxSupplierOnce<T>

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: Fn() -> T,