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 prism3_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 prism3_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 BoxSupplierOnce.

§Parameters
  • f - The closure to wrap
§Returns

A new BoxSupplierOnce<T> instance

§Examples
use prism3_function::{BoxSupplierOnce, SupplierOnce};

let once = BoxSupplierOnce::new(|| 42);
assert_eq!(once.get(), 42);
Examples found in repository?
examples/supplier_demo.rs (lines 118-121)
114fn demo_box_supplier_once() {
115    println!("--- BoxSupplierOnce ---");
116
117    // Basic usage
118    let once = BoxSupplierOnce::new(|| {
119        println!("  Expensive initialization");
120        42
121    });
122    println!("Value: {}", once.get());
123
124    // Moving captured values
125    let data = String::from("Hello, World!");
126    let once = BoxSupplierOnce::new(move || data);
127    println!("Moved data: {}", once.get());
128    println!();
129}

Trait Implementations§

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>
where Self: Sized + 'static, T: 'static,

Converts to BoxSupplierOnce. Read more
Source§

fn into_fn(self) -> impl FnOnce() -> T
where Self: Sized + 'static, T: 'static,

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> !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, 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.