Skip to main content

BoxSupplier

Struct BoxSupplier 

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

Source

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

Creates a new supplier.

Wraps the provided closure in the appropriate smart pointer type for this supplier implementation.

Source

pub fn new_with_name<F>(name: &str, f: F) -> Self
where 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.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: Fn() -> T + 'static,

Creates a new named supplier with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this supplier.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this supplier.

§Parameters
  • name - The name to set for this supplier
Source

pub fn constant(value: T) -> Self
where T: Clone + 'static,

Creates a supplier that returns a constant value.

Creates a supplier that always returns the same value. Useful for default values or placeholder implementations.

§Parameters
  • value - The constant value to return
§Returns

Returns a new supplier instance that returns the constant value.

Source

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

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

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for BoxSupplier<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Supplier<T> for BoxSupplier<T>

Source§

fn get(&self) -> T

Generates and returns a value. Read more
Source§

fn into_box(self) -> BoxSupplier<T>

Converts to BoxSupplier. Read more
Source§

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

Converts to RcSupplier. Read more
Source§

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

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

fn into_once(self) -> BoxSupplierOnce<T>
where Self: 'static,

Converts to BoxSupplierOnce. Read more
Source§

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

Converts to ArcSupplier. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BoxSupplier<T>

§

impl<T> !RefUnwindSafe for BoxSupplier<T>

§

impl<T> !Send for BoxSupplier<T>

§

impl<T> !Sync for BoxSupplier<T>

§

impl<T> Unpin for BoxSupplier<T>

§

impl<T> UnsafeUnpin for BoxSupplier<T>

§

impl<T> !UnwindSafe for BoxSupplier<T>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.