Skip to main content

BoxStatefulSupplier

Struct BoxStatefulSupplier 

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

Box-based single ownership supplier.

Uses Box<dyn FnMut() -> T> for single ownership scenarios. This is the most lightweight supplier with zero reference counting overhead.

§Ownership Model

Methods consume self (move semantics). When you call a method like map(), the original supplier is consumed and you get a new one:

use qubit_function::{BoxStatefulSupplier, StatefulSupplier};

let supplier = BoxStatefulSupplier::new(|| 10);
let mapped = supplier.map(|x| x * 2);
// supplier is no longer usable here

§Examples

§Counter

use qubit_function::{BoxStatefulSupplier, StatefulSupplier};

let mut counter = 0;
let mut supplier = BoxStatefulSupplier::new(move || {
    counter += 1;
    counter
});

assert_eq!(supplier.get(), 1);
assert_eq!(supplier.get(), 2);

§Method Chaining

use qubit_function::{BoxStatefulSupplier, StatefulSupplier};

let mut pipeline = BoxStatefulSupplier::new(|| 10)
    .map(|x| x * 2)
    .map(|x| x + 5);

assert_eq!(pipeline.get(), 25);

§Author

Haixing Hu

Implementations§

Source§

impl<T> BoxStatefulSupplier<T>

Source

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

Creates a new supplier.

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

Examples found in repository?
examples/suppliers/stateful_supplier_demo.rs (lines 84-87)
70fn demo_box_supplier_basics() {
71    println!("--- BoxSupplier Basics ---");
72
73    // Basic usage (Fn)
74    let supplier = BoxSupplier::new(|| 42);
75    println!("Basic: {}", supplier.get());
76
77    // Constant supplier (Fn)
78    let constant = BoxSupplier::constant(100);
79    println!("Constant: {}", constant.get());
80    println!("Constant: {}", constant.get());
81
82    // Stateful counter (FnMut) - use BoxStatefulSupplier
83    let mut counter = 0;
84    let mut counter_supplier = BoxStatefulSupplier::new(move || {
85        counter += 1;
86        counter
87    });
88    println!("Counter: {}", counter_supplier.get());
89    println!("Counter: {}", counter_supplier.get());
90    println!("Counter: {}", counter_supplier.get());
91    println!();
92}
93
94fn demo_box_supplier_methods() {
95    println!("--- BoxStatefulSupplier Methods ---");
96
97    // Map (FnMut)
98    let mut counter = 0;
99    let mut mapped = BoxStatefulSupplier::new(move || {
100        counter += 1;
101        counter * 10
102    })
103    .map(|x| x + 5);
104    println!("Mapped: {}", mapped.get());
105    println!("Mapped: {}", mapped.get());
106
107    // Filter (FnMut)
108    let mut counter = 0;
109    let mut filtered = BoxStatefulSupplier::new(move || {
110        counter += 1;
111        counter
112    })
113    .filter(is_even_i32);
114    println!("Filtered (odd): {:?}", filtered.get());
115    println!("Filtered (even): {:?}", filtered.get());
116
117    // Zip (Fn)
118    let first = BoxSupplier::new(|| 42);
119    let second = BoxSupplier::new(|| "hello");
120    let zipped = first.zip(second);
121    println!("Zipped: {:?}", zipped.get());
122
123    // Memoize (FnMut)
124    let mut call_count = 0;
125    let mut memoized = BoxStatefulSupplier::new(move || {
126        call_count += 1;
127        println!("  Expensive computation #{}", call_count);
128        42
129    })
130    .memoize();
131    println!("First call: {}", memoized.get());
132    println!("Second call (cached): {}", memoized.get());
133    println!();
134}
Source

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

Clears the name of 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) -> BoxStatefulSupplier<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);
Examples found in repository?
examples/suppliers/stateful_supplier_demo.rs (line 103)
94fn demo_box_supplier_methods() {
95    println!("--- BoxStatefulSupplier Methods ---");
96
97    // Map (FnMut)
98    let mut counter = 0;
99    let mut mapped = BoxStatefulSupplier::new(move || {
100        counter += 1;
101        counter * 10
102    })
103    .map(|x| x + 5);
104    println!("Mapped: {}", mapped.get());
105    println!("Mapped: {}", mapped.get());
106
107    // Filter (FnMut)
108    let mut counter = 0;
109    let mut filtered = BoxStatefulSupplier::new(move || {
110        counter += 1;
111        counter
112    })
113    .filter(is_even_i32);
114    println!("Filtered (odd): {:?}", filtered.get());
115    println!("Filtered (even): {:?}", filtered.get());
116
117    // Zip (Fn)
118    let first = BoxSupplier::new(|| 42);
119    let second = BoxSupplier::new(|| "hello");
120    let zipped = first.zip(second);
121    println!("Zipped: {:?}", zipped.get());
122
123    // Memoize (FnMut)
124    let mut call_count = 0;
125    let mut memoized = BoxStatefulSupplier::new(move || {
126        call_count += 1;
127        println!("  Expensive computation #{}", call_count);
128        42
129    })
130    .memoize();
131    println!("First call: {}", memoized.get());
132    println!("Second call (cached): {}", memoized.get());
133    println!();
134}
More examples
Hide additional examples
examples/suppliers/closure_supplier_ops_demo.rs (line 72)
29fn main() {
30    println!("=== Closure StatefulSupplier Operations Demo ===\n");
31
32    // 1. FnMut closure using map
33    println!("1. FnMut closure using map:");
34    let mut counter = 0;
35    let mut mapped = (move || {
36        counter += 1;
37        counter
38    })
39    .map(|x| x * 2);
40
41    println!("   First call: {}", mapped.get());
42    println!("   Second call: {}\n", mapped.get());
43
44    // 2. FnMut closure using filter
45    println!("2. FnMut closure using filter:");
46    let mut counter2 = 0;
47    let mut filtered = (move || {
48        counter2 += 1;
49        counter2
50    })
51    .filter(is_even_i32);
52
53    println!("   First call (odd number): {:?}", filtered.get());
54    println!("   Second call (even number): {:?}\n", filtered.get());
55
56    // 3. FnMut closure using memoize
57    println!("3. FnMut closure using memoize:");
58    let mut call_count = 0;
59    let mut memoized = (move || {
60        call_count += 1;
61        println!("   Underlying function called {} times", call_count);
62        42
63    })
64    .memoize();
65
66    println!("   First call: {}", memoized.get());
67    println!("   Second call: {}", memoized.get());
68    println!("   Third call: {}\n", memoized.get());
69
70    // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
71    println!("4. Fn closure using map:");
72    let mut mapped_stateless = (|| 10).map(|x| x * 3).map(|x| x + 5);
73    println!("   Result: {}\n", mapped_stateless.get());
74
75    // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
76    println!("5. Fn closure using filter:");
77    let mut filtered_stateless = (|| 42).filter(is_even_i32);
78    println!("   Filtered even number: {:?}\n", filtered_stateless.get());
79
80    // 6. Chained operations
81    println!("6. Chained operations:");
82    let mut counter3 = 0;
83    let mut chained = (move || {
84        counter3 += 1;
85        counter3
86    })
87    .map(|x| x * 2)
88    .filter(greater_than_five)
89    .map(|opt: Option<i32>| opt.unwrap_or(0));
90
91    println!("   First call: {}", chained.get()); // 2, filtered out
92    println!("   Second call: {}", chained.get()); // 4, filtered out
93    println!("   Third call: {}", chained.get()); // 6, passed
94    println!("   Fourth call: {}\n", chained.get()); // 8, passed
95
96    println!("=== Demo completed ===");
97}
Source

pub fn filter<P>(self, predicate: P) -> BoxStatefulSupplier<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::predicates::BoxPredicate;
use qubit_function::{Predicate, suppliers::*};

let supplier = BoxSupplier::new(|| 42);
let is_even = BoxPredicate::new(|x: &i32| *x % 2 == 0);
let filtered = supplier.filter(is_even);

assert_eq!(filtered.get(), Some(42));
Examples found in repository?
examples/suppliers/stateful_supplier_demo.rs (line 113)
94fn demo_box_supplier_methods() {
95    println!("--- BoxStatefulSupplier Methods ---");
96
97    // Map (FnMut)
98    let mut counter = 0;
99    let mut mapped = BoxStatefulSupplier::new(move || {
100        counter += 1;
101        counter * 10
102    })
103    .map(|x| x + 5);
104    println!("Mapped: {}", mapped.get());
105    println!("Mapped: {}", mapped.get());
106
107    // Filter (FnMut)
108    let mut counter = 0;
109    let mut filtered = BoxStatefulSupplier::new(move || {
110        counter += 1;
111        counter
112    })
113    .filter(is_even_i32);
114    println!("Filtered (odd): {:?}", filtered.get());
115    println!("Filtered (even): {:?}", filtered.get());
116
117    // Zip (Fn)
118    let first = BoxSupplier::new(|| 42);
119    let second = BoxSupplier::new(|| "hello");
120    let zipped = first.zip(second);
121    println!("Zipped: {:?}", zipped.get());
122
123    // Memoize (FnMut)
124    let mut call_count = 0;
125    let mut memoized = BoxStatefulSupplier::new(move || {
126        call_count += 1;
127        println!("  Expensive computation #{}", call_count);
128        42
129    })
130    .memoize();
131    println!("First call: {}", memoized.get());
132    println!("Second call (cached): {}", memoized.get());
133    println!();
134}
More examples
Hide additional examples
examples/suppliers/closure_supplier_ops_demo.rs (line 88)
29fn main() {
30    println!("=== Closure StatefulSupplier Operations Demo ===\n");
31
32    // 1. FnMut closure using map
33    println!("1. FnMut closure using map:");
34    let mut counter = 0;
35    let mut mapped = (move || {
36        counter += 1;
37        counter
38    })
39    .map(|x| x * 2);
40
41    println!("   First call: {}", mapped.get());
42    println!("   Second call: {}\n", mapped.get());
43
44    // 2. FnMut closure using filter
45    println!("2. FnMut closure using filter:");
46    let mut counter2 = 0;
47    let mut filtered = (move || {
48        counter2 += 1;
49        counter2
50    })
51    .filter(is_even_i32);
52
53    println!("   First call (odd number): {:?}", filtered.get());
54    println!("   Second call (even number): {:?}\n", filtered.get());
55
56    // 3. FnMut closure using memoize
57    println!("3. FnMut closure using memoize:");
58    let mut call_count = 0;
59    let mut memoized = (move || {
60        call_count += 1;
61        println!("   Underlying function called {} times", call_count);
62        42
63    })
64    .memoize();
65
66    println!("   First call: {}", memoized.get());
67    println!("   Second call: {}", memoized.get());
68    println!("   Third call: {}\n", memoized.get());
69
70    // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
71    println!("4. Fn closure using map:");
72    let mut mapped_stateless = (|| 10).map(|x| x * 3).map(|x| x + 5);
73    println!("   Result: {}\n", mapped_stateless.get());
74
75    // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
76    println!("5. Fn closure using filter:");
77    let mut filtered_stateless = (|| 42).filter(is_even_i32);
78    println!("   Filtered even number: {:?}\n", filtered_stateless.get());
79
80    // 6. Chained operations
81    println!("6. Chained operations:");
82    let mut counter3 = 0;
83    let mut chained = (move || {
84        counter3 += 1;
85        counter3
86    })
87    .map(|x| x * 2)
88    .filter(greater_than_five)
89    .map(|opt: Option<i32>| opt.unwrap_or(0));
90
91    println!("   First call: {}", chained.get()); // 2, filtered out
92    println!("   Second call: {}", chained.get()); // 4, filtered out
93    println!("   Third call: {}", chained.get()); // 6, passed
94    println!("   Fourth call: {}\n", chained.get()); // 8, passed
95
96    println!("=== Demo completed ===");
97}
Source

pub fn zip<U, S>(self, other: S) -> BoxStatefulSupplier<(T, U)>
where T: 'static, S: StatefulSupplier<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"));
Source

pub fn memoize(self) -> BoxStatefulSupplier<T>
where T: Clone + 'static,

Creates a memoizing supplier.

Returns a new supplier that caches the first value it produces. All subsequent calls return the cached value.

§Returns

A new memoized BoxStatefulSupplier<T>

§Examples
use qubit_function::{BoxStatefulSupplier, StatefulSupplier};

let mut call_count = 0;
let mut memoized = BoxStatefulSupplier::new(move || {
    call_count += 1;
    42
}).memoize();

assert_eq!(memoized.get(), 42); // Calls underlying function
assert_eq!(memoized.get(), 42); // Returns cached value
Examples found in repository?
examples/suppliers/stateful_supplier_demo.rs (line 130)
94fn demo_box_supplier_methods() {
95    println!("--- BoxStatefulSupplier Methods ---");
96
97    // Map (FnMut)
98    let mut counter = 0;
99    let mut mapped = BoxStatefulSupplier::new(move || {
100        counter += 1;
101        counter * 10
102    })
103    .map(|x| x + 5);
104    println!("Mapped: {}", mapped.get());
105    println!("Mapped: {}", mapped.get());
106
107    // Filter (FnMut)
108    let mut counter = 0;
109    let mut filtered = BoxStatefulSupplier::new(move || {
110        counter += 1;
111        counter
112    })
113    .filter(is_even_i32);
114    println!("Filtered (odd): {:?}", filtered.get());
115    println!("Filtered (even): {:?}", filtered.get());
116
117    // Zip (Fn)
118    let first = BoxSupplier::new(|| 42);
119    let second = BoxSupplier::new(|| "hello");
120    let zipped = first.zip(second);
121    println!("Zipped: {:?}", zipped.get());
122
123    // Memoize (FnMut)
124    let mut call_count = 0;
125    let mut memoized = BoxStatefulSupplier::new(move || {
126        call_count += 1;
127        println!("  Expensive computation #{}", call_count);
128        42
129    })
130    .memoize();
131    println!("First call: {}", memoized.get());
132    println!("Second call (cached): {}", memoized.get());
133    println!();
134}

Trait Implementations§

Source§

impl<T> Debug for BoxStatefulSupplier<T>

Source§

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

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

impl<T> Display for BoxStatefulSupplier<T>

Source§

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

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

impl<T> StatefulSupplier<T> for BoxStatefulSupplier<T>

Source§

fn get(&mut self) -> T

Generates and returns the next value. Read more
Source§

fn into_box(self) -> BoxStatefulSupplier<T>

Converts to BoxStatefulSupplier. Read more
Source§

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

Converts to RcStatefulSupplier. Read more
Source§

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

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

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

Converts to BoxSupplierOnce. Read more
Source§

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

Converts to ArcStatefulSupplier. 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> 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.