Skip to main content

BoxSupplierOnce

Struct BoxSupplierOnce 

Source
pub struct BoxSupplierOnce<T> { /* private fields */ }
Expand description

Box-based one-time supplier.

Uses Box<dyn FnOnce() -> T> for one-time value generation. Can only call get() once, consuming the supplier.

§Examples

§Lazy Initialization

use qubit_function::{BoxSupplierOnce, SupplierOnce};

let once = BoxSupplierOnce::new(|| {
    println!("Expensive initialization");
    42
});

let value = once.get(); // Prints: Expensive initialization
assert_eq!(value, 42);

§Moving Captured Values

use qubit_function::{BoxSupplierOnce, SupplierOnce};

let resource = String::from("data");
let once = BoxSupplierOnce::new(move || resource);

let value = once.get();
assert_eq!(value, "data");

§Author

Haixing Hu

Implementations§

Source§

impl<T> BoxSupplierOnce<T>

Source

pub fn new<F>(f: F) -> Self
where F: FnOnce() -> 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: FnOnce() -> 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: FnOnce() -> 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) -> BoxSupplierOnce<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) -> BoxSupplierOnce<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) -> BoxSupplierOnce<(T, U)>
where T: 'static, S: SupplierOnce<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 BoxSupplierOnce<T>

Source§

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

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

impl<T> Display for BoxSupplierOnce<T>

Source§

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

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

impl<T> SupplierOnce<T> for BoxSupplierOnce<T>

Source§

fn get(self) -> T

Generates and returns the value, consuming self. Read more
Source§

fn into_box(self) -> BoxSupplierOnce<T>

Converts to BoxSupplierOnce. Read more
Source§

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

Converts the supplier to a Box<dyn FnOnce() -> T>. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BoxSupplierOnce<T>

§

impl<T> !RefUnwindSafe for BoxSupplierOnce<T>

§

impl<T> !Send for BoxSupplierOnce<T>

§

impl<T> !Sync for BoxSupplierOnce<T>

§

impl<T> Unpin for BoxSupplierOnce<T>

§

impl<T> UnsafeUnpin for BoxSupplierOnce<T>

§

impl<T> !UnwindSafe for BoxSupplierOnce<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.