pub struct BoxReadonlySupplier<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 prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let supplier = BoxReadonlySupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here§Examples
§Constant Factory
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let factory = BoxReadonlySupplier::new(|| 42);
assert_eq!(factory.get(), 42);
assert_eq!(factory.get(), 42);§Method Chaining
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let pipeline = BoxReadonlySupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(pipeline.get(), 25);§Author
Haixing Hu
Implementations§
Source§impl<T> BoxReadonlySupplier<T>where
T: 'static,
impl<T> BoxReadonlySupplier<T>where
T: 'static,
Sourcepub fn constant(value: T) -> Selfwhere
T: Clone + 'static,
pub fn constant(value: T) -> Selfwhere
T: Clone + 'static,
Creates a constant supplier.
Returns a supplier that always produces the same value (via cloning).
§Parameters
value- The constant value to return
§Returns
A constant supplier
§Examples
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let constant = BoxReadonlySupplier::constant(42);
assert_eq!(constant.get(), 42);
assert_eq!(constant.get(), 42);Sourcepub fn map<U, M>(self, mapper: M) -> BoxReadonlySupplier<U>where
M: Transformer<T, U> + 'static,
U: 'static,
pub fn map<U, M>(self, mapper: M) -> BoxReadonlySupplier<U>where
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 BoxReadonlySupplier<U>
§Examples
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let mapped = BoxReadonlySupplier::new(|| 10)
.map(|x| x * 2)
.map(|x| x + 5);
assert_eq!(mapped.get(), 25);Sourcepub fn filter<P>(self, predicate: P) -> BoxReadonlySupplier<Option<T>>
pub fn filter<P>(self, predicate: P) -> BoxReadonlySupplier<Option<T>>
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 BoxReadonlySupplier<Option<T>>
§Examples
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let filtered = BoxReadonlySupplier::new(|| 42)
.filter(|x| x % 2 == 0);
assert_eq!(filtered.get(), Some(42));Sourcepub fn zip<U>(
self,
other: BoxReadonlySupplier<U>,
) -> BoxReadonlySupplier<(T, U)>where
U: 'static,
pub fn zip<U>(
self,
other: BoxReadonlySupplier<U>,
) -> BoxReadonlySupplier<(T, U)>where
U: 'static,
Combines this supplier with another, producing a tuple.
Consumes both suppliers and returns a new supplier that
produces (T, U) tuples.
§Parameters
other- The other supplier to combine with
§Returns
A new BoxReadonlySupplier<(T, U)>
§Examples
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};
let first = BoxReadonlySupplier::new(|| 42);
let second = BoxReadonlySupplier::new(|| "hello");
let zipped = first.zip(second);
assert_eq!(zipped.get(), (42, "hello"));Trait Implementations§
Source§impl<T> ReadonlySupplier<T> for BoxReadonlySupplier<T>
impl<T> ReadonlySupplier<T> for BoxReadonlySupplier<T>
Source§fn into_box(self) -> BoxReadonlySupplier<T>where
T: 'static,
fn into_box(self) -> BoxReadonlySupplier<T>where
T: 'static,
BoxReadonlySupplier. Read moreSource§fn into_rc(self) -> RcReadonlySupplier<T>where
T: 'static,
fn into_rc(self) -> RcReadonlySupplier<T>where
T: 'static,
RcReadonlySupplier. Read more