pub trait ReadonlySupplier<T> {
// Required method
fn get(&self) -> T;
// Provided methods
fn into_box(self) -> BoxReadonlySupplier<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_rc(self) -> RcReadonlySupplier<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_arc(self) -> ArcReadonlySupplier<T>
where Self: Sized + Send + Sync + 'static,
T: Send + 'static { ... }
fn into_fn(self) -> impl FnMut() -> T
where Self: Sized { ... }
fn to_box(&self) -> BoxReadonlySupplier<T>
where Self: Clone + 'static,
T: 'static { ... }
fn to_rc(&self) -> RcReadonlySupplier<T>
where Self: Clone + 'static,
T: 'static { ... }
fn to_arc(&self) -> ArcReadonlySupplier<T>
where Self: Clone + Send + Sync + 'static,
T: Send + 'static { ... }
fn to_fn(&self) -> impl FnMut() -> T
where Self: Clone { ... }
}Expand description
Read-only supplier trait: generates values without modifying state.
The core abstraction for stateless value generation. Unlike
Supplier<T>, it uses &self instead of &mut self, enabling
usage in read-only contexts and lock-free concurrent access.
§Key Characteristics
- No input parameters: Pure value generation
- Read-only access: Uses
&self, doesn’t modify state - Returns ownership: Returns
T(not&T) to avoid lifetime issues - Lock-free concurrency:
Arcimplementation doesn’t needMutex
§Automatically Implemented for Closures
All Fn() -> T closures automatically implement this trait,
enabling seamless integration with both raw closures and
wrapped supplier types.
§Examples
§Using with Generic Functions
use prism3_function::{ReadonlySupplier, BoxReadonlySupplier};
fn call_twice<S: ReadonlySupplier<i32>>(supplier: &S)
-> (i32, i32)
{
(supplier.get(), supplier.get())
}
let s = BoxReadonlySupplier::new(|| 42);
assert_eq!(call_twice(&s), (42, 42));
let closure = || 100;
assert_eq!(call_twice(&closure), (100, 100));§Stateless Factory
use prism3_function::ReadonlySupplier;
struct User {
name: String,
}
impl User {
fn new() -> Self {
User {
name: String::from("Default"),
}
}
}
let factory = || User::new();
let user1 = factory.get();
let user2 = factory.get();
// Each call creates a new User instance§Author
Haixing Hu
Required Methods§
Sourcefn get(&self) -> T
fn get(&self) -> T
Generates and returns a value.
Executes the underlying function and returns the generated
value. Uses &self because the supplier doesn’t modify its
own state.
§Returns
The generated value of type T
§Examples
use prism3_function::{ReadonlySupplier, BoxReadonlySupplier};
let supplier = BoxReadonlySupplier::new(|| 42);
assert_eq!(supplier.get(), 42);
assert_eq!(supplier.get(), 42);Provided Methods§
Sourcefn into_box(self) -> BoxReadonlySupplier<T>where
Self: Sized + 'static,
T: 'static,
fn into_box(self) -> BoxReadonlySupplier<T>where
Self: Sized + 'static,
T: 'static,
Converts to BoxReadonlySupplier.
This method has a default implementation that wraps the
supplier in a BoxReadonlySupplier. Custom implementations
can override this method for optimization purposes.
§Returns
A new BoxReadonlySupplier<T> instance
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let boxed = closure.into_box();
assert_eq!(boxed.get(), 42);Sourcefn into_rc(self) -> RcReadonlySupplier<T>where
Self: Sized + 'static,
T: 'static,
fn into_rc(self) -> RcReadonlySupplier<T>where
Self: Sized + 'static,
T: 'static,
Converts to RcReadonlySupplier.
This method has a default implementation that wraps the
supplier in an RcReadonlySupplier. Custom implementations
can override this method for optimization purposes.
§Returns
A new RcReadonlySupplier<T> instance
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let rc = closure.into_rc();
assert_eq!(rc.get(), 42);Sourcefn into_arc(self) -> ArcReadonlySupplier<T>
fn into_arc(self) -> ArcReadonlySupplier<T>
Converts to ArcReadonlySupplier.
This method has a default implementation that wraps the
supplier in an ArcReadonlySupplier. Custom implementations
can override this method for optimization purposes.
§Returns
A new ArcReadonlySupplier<T> instance
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let arc = closure.into_arc();
assert_eq!(arc.get(), 42);Sourcefn into_fn(self) -> impl FnMut() -> Twhere
Self: Sized,
fn into_fn(self) -> impl FnMut() -> Twhere
Self: Sized,
Converts to a closure implementing FnMut() -> T.
This method has a default implementation that wraps the supplier in a closure. Custom implementations can override this method for optimization purposes.
§Returns
A closure implementing FnMut() -> T
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let mut fn_mut = closure.into_fn();
assert_eq!(fn_mut(), 42);
assert_eq!(fn_mut(), 42);Sourcefn to_box(&self) -> BoxReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
fn to_box(&self) -> BoxReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
Converts to BoxReadonlySupplier by cloning.
This method clones the supplier and wraps it in a
BoxReadonlySupplier. Requires Self: Clone. Custom
implementations can override this method for optimization.
§Returns
A new BoxReadonlySupplier<T> instance
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let boxed = closure.to_box();
assert_eq!(boxed.get(), 42);Sourcefn to_rc(&self) -> RcReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
fn to_rc(&self) -> RcReadonlySupplier<T>where
Self: Clone + 'static,
T: 'static,
Converts to RcReadonlySupplier by cloning.
This method clones the supplier and wraps it in an
RcReadonlySupplier. Requires Self: Clone. Custom
implementations can override this method for optimization.
§Returns
A new RcReadonlySupplier<T> instance
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let rc = closure.to_rc();
assert_eq!(rc.get(), 42);Sourcefn to_arc(&self) -> ArcReadonlySupplier<T>
fn to_arc(&self) -> ArcReadonlySupplier<T>
Converts to ArcReadonlySupplier by cloning.
This method clones the supplier and wraps it in an
ArcReadonlySupplier. Requires Self: Clone + Send + Sync.
Custom implementations can override this method for
optimization.
§Returns
A new ArcReadonlySupplier<T> instance
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let arc = closure.to_arc();
assert_eq!(arc.get(), 42);Sourcefn to_fn(&self) -> impl FnMut() -> Twhere
Self: Clone,
fn to_fn(&self) -> impl FnMut() -> Twhere
Self: Clone,
Converts to a closure by cloning.
This method clones the supplier and wraps it in a closure
implementing FnMut() -> T. Requires Self: Clone. Custom
implementations can override this method for optimization.
§Returns
A closure implementing FnMut() -> T
§Examples
use prism3_function::ReadonlySupplier;
let closure = || 42;
let mut fn_mut = closure.to_fn();
assert_eq!(fn_mut(), 42);
assert_eq!(fn_mut(), 42);