pub struct BoxStatefulSupplier<T> { /* private fields */ }Expand description
Box-based single ownership supplier.
Uses Box<dyn FnMut() -> T> for single ownership scenarios.
This is the most lightweight supplier with zero reference
counting overhead.
§Ownership Model
Methods consume self (move semantics). When you call a method
like map(), the original supplier is consumed and you get a new
one:
use qubit_function::{BoxStatefulSupplier, Supplier};
let supplier = BoxStatefulSupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here§Examples
§Counter
use qubit_function::{BoxStatefulSupplier, Supplier};
let mut counter = 0;
let mut supplier = BoxStatefulSupplier::new(move || {
counter += 1;
counter
});
assert_eq!(supplier.get(), 1);
assert_eq!(supplier.get(), 2);§Method Chaining
use qubit_function::{BoxStatefulSupplier, Supplier};
let mut pipeline = BoxStatefulSupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(pipeline.get(), 25);§Author
Haixing Hu
Implementations§
Source§impl<T> BoxStatefulSupplier<T>
impl<T> BoxStatefulSupplier<T>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> T + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnMut() -> 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: FnMut() -> T + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: FnMut() -> 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: FnMut() -> T + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: FnMut() -> 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) -> BoxStatefulSupplier<U>where
T: 'static,
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(self, mapper: M) -> BoxStatefulSupplier<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) -> BoxStatefulSupplier<Option<T>>where
T: 'static,
P: Predicate<T> + 'static,
pub fn filter<P>(self, predicate: P) -> BoxStatefulSupplier<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) -> BoxStatefulSupplier<(T, U)>where
T: 'static,
S: StatefulSupplier<U> + 'static,
U: 'static,
pub fn zip<U, S>(self, other: S) -> BoxStatefulSupplier<(T, U)>where
T: 'static,
S: StatefulSupplier<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"));Sourcepub fn memoize(self) -> BoxStatefulSupplier<T>where
T: Clone + 'static,
pub fn memoize(self) -> BoxStatefulSupplier<T>where
T: Clone + 'static,
Creates a memoizing supplier.
Returns a new supplier that caches the first value it produces. All subsequent calls return the cached value.
§Returns
A new memoized BoxStatefulSupplier<T>
§Examples
use qubit_function::{BoxStatefulSupplier, Supplier};
let mut call_count = 0;
let mut memoized = BoxStatefulSupplier::new(move || {
call_count += 1;
42
}).memoize();
assert_eq!(memoized.get(), 42); // Calls underlying function
assert_eq!(memoized.get(), 42); // Returns cached valueTrait Implementations§
Source§impl<T> Debug for BoxStatefulSupplier<T>
impl<T> Debug for BoxStatefulSupplier<T>
Source§impl<T> Display for BoxStatefulSupplier<T>
impl<T> Display for BoxStatefulSupplier<T>
Source§impl<T> StatefulSupplier<T> for BoxStatefulSupplier<T>
impl<T> StatefulSupplier<T> for BoxStatefulSupplier<T>
Source§fn into_box(self) -> BoxStatefulSupplier<T>
fn into_box(self) -> BoxStatefulSupplier<T>
BoxStatefulSupplier. Read moreSource§fn into_rc(self) -> RcStatefulSupplier<T>where
Self: 'static,
fn into_rc(self) -> RcStatefulSupplier<T>where
Self: 'static,
RcStatefulSupplier. Read moreSource§fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
fn into_once(self) -> BoxSupplierOnce<T>where
Self: 'static,
BoxSupplierOnce. Read more