pub struct BoxSupplierOnce<T> { /* private fields */ }Expand description
Box-based one-time supplier.
Uses Box<dyn FnOnce() -> T> for one-time value generation.
Can only call get() once, consuming the supplier.
§Examples
§Lazy Initialization
use prism3_function::{BoxSupplierOnce, SupplierOnce};
let once = BoxSupplierOnce::new(|| {
println!("Expensive initialization");
42
});
let value = once.get(); // Prints: Expensive initialization
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
Implementations§
Source§impl<T> BoxSupplierOnce<T>
impl<T> BoxSupplierOnce<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnOnce() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnOnce() -> T + 'static,
Creates a new BoxSupplierOnce.
§Parameters
f- The closure to wrap
§Returns
A new BoxSupplierOnce<T> instance
§Examples
use prism3_function::{BoxSupplierOnce, SupplierOnce};
let once = BoxSupplierOnce::new(|| 42);
assert_eq!(once.get(), 42);Examples found in repository?
examples/supplier_demo.rs (lines 118-121)
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}Trait Implementations§
Source§impl<T> SupplierOnce<T> for BoxSupplierOnce<T>
impl<T> SupplierOnce<T> for BoxSupplierOnce<T>
Source§fn into_box_once(self) -> BoxSupplierOnce<T>where
T: 'static,
fn into_box_once(self) -> BoxSupplierOnce<T>where
T: 'static,
Converts to
BoxSupplierOnce. Read moreAuto Trait Implementations§
impl<T> Freeze for BoxSupplierOnce<T>
impl<T> !RefUnwindSafe for BoxSupplierOnce<T>
impl<T> !Send for BoxSupplierOnce<T>
impl<T> !Sync for BoxSupplierOnce<T>
impl<T> Unpin for BoxSupplierOnce<T>
impl<T> !UnwindSafe for BoxSupplierOnce<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more