Expand description
§SupplierOnce Types
Provides one-time supplier implementations that generate and return values without taking any input parameters, consuming themselves in the process.
§Overview
A SupplierOnce is a functional abstraction similar to
Supplier, but can only be called once. The get() method
consumes self, ensuring the supplier cannot be reused.
§Key Characteristics
- Single use: Can only call
get()once - Consumes self: The method takes ownership of
self - Holds
FnOnce: Can capture and move non-cloneable values - Type-system guaranteed: Prevents multiple calls at compile time
§Use Cases
- Lazy initialization: Delay expensive computation until needed
- One-time resource consumption: Generate value by consuming a resource
- Move-only closures: Hold closures that capture moved values
§Examples
§Lazy Initialization
use prism3_function::{BoxSupplierOnce, SupplierOnce};
let once = BoxSupplierOnce::new(|| {
println!("Expensive initialization");
42
});
let value = once.get(); // Only initializes once
assert_eq!(value, 42);§Moving Captured Values
use prism3_function::{BoxSupplierOnce, SupplierOnce};
let resource = String::from("data");
let once = BoxSupplierOnce::new(move || resource);
let value = once.get();
assert_eq!(value, "data");§Author
Haixing Hu
Structs§
- BoxSupplier
Once - Box-based one-time supplier.
Traits§
- Supplier
Once - One-time supplier trait: generates a value consuming self.