BoxReadonlySupplier

Struct BoxReadonlySupplier 

Source
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,

Source

pub fn new<F>(f: F) -> Self
where F: Fn() -> T + 'static,

Creates a new BoxReadonlySupplier.

§Parameters
  • f - The closure to wrap
§Returns

A new BoxReadonlySupplier<T> instance

§Examples
use prism3_function::{BoxReadonlySupplier, ReadonlySupplier};

let supplier = BoxReadonlySupplier::new(|| 42);
assert_eq!(supplier.get(), 42);
Source

pub fn constant(value: T) -> Self
where 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);
Source

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 implementing Transformer<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);
Source

pub fn filter<P>(self, predicate: P) -> BoxReadonlySupplier<Option<T>>
where P: Fn(&T) -> bool + '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 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));
Source

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>

Source§

fn get(&self) -> T

Generates and returns a value. Read more
Source§

fn into_box(self) -> BoxReadonlySupplier<T>
where T: 'static,

Converts to BoxReadonlySupplier. Read more
Source§

fn into_rc(self) -> RcReadonlySupplier<T>
where T: 'static,

Converts to RcReadonlySupplier. Read more
Source§

fn into_fn(self) -> impl FnMut() -> T

Converts to a closure implementing FnMut() -> T. Read more
Source§

fn into_arc(self) -> ArcReadonlySupplier<T>
where Self: Sized + Send + Sync + 'static, T: Send + 'static,

Converts to ArcReadonlySupplier. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.