pub struct BoxSupplier<T> { /* private fields */ }Expand description
Box-based single ownership read-only supplier.
Uses Box<dyn Fn() -> T> for single ownership scenarios. This
is the most lightweight read-only supplier with zero reference
counting overhead.
§Ownership Model
Methods consume self (move semantics) or borrow &self for
read-only operations. When you call methods like map(), the
original supplier is consumed and you get a new one:
use qubit_function::{BoxSupplier, Supplier};
let supplier = BoxSupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here§Examples
§Constant Factory
use qubit_function::{BoxSupplier, Supplier};
let factory = BoxSupplier::new(|| 42);
assert_eq!(factory.get(), 42);
assert_eq!(factory.get(), 42);§Method Chaining
use qubit_function::{BoxSupplier, Supplier};
let pipeline = BoxSupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(pipeline.get(), 25);§Author
Haixing Hu
Implementations§
Source§impl<T> BoxSupplier<T>
impl<T> BoxSupplier<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: Fn() -> 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: Fn() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn() -> 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: Fn() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn() -> 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) -> BoxSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(self, mapper: M) -> BoxSupplier<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) -> BoxSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(self, predicate: P) -> BoxSupplier<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) -> BoxSupplier<(T, U)>where
T: 'static,
S: Supplier<U> + 'static,
U: 'static,
pub fn zip<U, S>(self, other: S) -> BoxSupplier<(T, U)>where
T: 'static,
S: Supplier<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"));Trait Implementations§
Source§impl<T> Debug for BoxSupplier<T>
impl<T> Debug for BoxSupplier<T>
Source§impl<T> Display for BoxSupplier<T>
impl<T> Display for BoxSupplier<T>
Source§impl<T> Supplier<T> for BoxSupplier<T>
impl<T> Supplier<T> for BoxSupplier<T>
Source§fn into_box(self) -> BoxSupplier<T>
fn into_box(self) -> BoxSupplier<T>
BoxSupplier. Read moreSource§fn into_rc(self) -> RcSupplier<T>where
Self: 'static,
fn into_rc(self) -> RcSupplier<T>where
Self: 'static,
RcSupplier. Read moreSource§fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
BoxSupplierOnce. Read more