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 qubit_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 qubit_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 supplier.
Wraps the provided closure in the appropriate smart pointer type for this supplier implementation.
Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnOnce() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnOnce() -> T + 'static,
Creates a new named supplier.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnOnce() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnOnce() -> T + 'static,
Creates a new named supplier with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn map<U, M>(self, mapper: M) -> BoxSupplierOnce<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(self, mapper: M) -> BoxSupplierOnce<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
Maps the output using a transformation function.
Consumes self and returns a new supplier that applies the mapper to each output.
§Parameters
mapper- The transformer to apply to the output. Can be a closure, function pointer, or any type implementingTransformer<T, U>.
§Returns
A new mapped supplier
§Examples
use qubit_function::suppliers::*;
let supplier = BoxSupplier::new(|| 10);
let mapped = supplier
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(mapped.get(), 25);Sourcepub fn filter<P>(self, predicate: P) -> BoxSupplierOnce<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(self, predicate: P) -> BoxSupplierOnce<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
Filters output based on a predicate.
Returns a new supplier that returns Some(value) if the
predicate is satisfied, None otherwise.
§Parameters
predicate- The predicate to test the supplied value
§Returns
A new filtered supplier
§Examples
use qubit_function::suppliers::*;
let supplier = BoxSupplier::new(|| 42);
let filtered = supplier.filter(|x| x % 2 == 0);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U, S>(self, other: S) -> BoxSupplierOnce<(T, U)>where
T: 'static,
S: SupplierOnce<U> + 'static,
U: 'static,
pub fn zip<U, S>(self, other: S) -> BoxSupplierOnce<(T, U)>where
T: 'static,
S: SupplierOnce<U> + 'static,
U: 'static,
Combines this supplier with another, producing a tuple.
Consumes both suppliers and returns a new supplier that produces tuples.
§Parameters
other- The other supplier to combine with
§Returns
A new supplier that produces tuples
§Examples
use qubit_function::suppliers::*;
let first = BoxSupplier::new(|| 42);
let second = BoxSupplier::new(|| "hello");
let zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));