pub trait ReadonlyConsumer<T> {
// Required methods
fn accept(&self, value: &T);
fn into_box(self) -> BoxReadonlyConsumer<T>
where Self: Sized + 'static,
T: 'static;
fn into_rc(self) -> RcReadonlyConsumer<T>
where Self: Sized + 'static,
T: 'static;
fn into_arc(self) -> ArcReadonlyConsumer<T>
where Self: Sized + Send + Sync + 'static,
T: Send + Sync + 'static;
fn into_fn(self) -> impl Fn(&T)
where Self: Sized + 'static,
T: 'static;
}Expand description
ReadonlyConsumer trait - Unified readonly consumer interface
Defines the core behavior of all readonly consumer types. Unlike Consumer, ReadonlyConsumer
neither modifies its own state nor modifies input values, making it a completely immutable operation.
§Auto-implementation
- All closures implementing
Fn(&T) BoxReadonlyConsumer<T>,ArcReadonlyConsumer<T>,RcReadonlyConsumer<T>
§Features
- Unified Interface: All readonly consumer types share the same
acceptmethod signature - Auto-implementation: Closures automatically implement this trait with zero overhead
- Type Conversion: Easy conversion between different ownership models
- Generic Programming: Write functions that work with any readonly consumer type
- No Interior Mutability: No need for Mutex or RefCell, more efficient
§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};
fn apply_consumer<C: ReadonlyConsumer<i32>>(consumer: &C, value: &i32) {
consumer.accept(value);
}
let box_con = BoxReadonlyConsumer::new(|x: &i32| {
println!("Value: {}", x);
});
apply_consumer(&box_con, &5);§Author
Hu Haixing
Required Methods§
Sourcefn accept(&self, value: &T)
fn accept(&self, value: &T)
Execute readonly consumption operation
Performs an operation on the given reference. The operation typically reads input values or produces side effects, but neither modifies the input value nor the consumer’s own state.
§Parameters
value- Reference to the value to consume
§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};
let consumer = BoxReadonlyConsumer::new(|x: &i32| println!("{}", x));
consumer.accept(&5);Sourcefn into_box(self) -> BoxReadonlyConsumer<T>where
Self: Sized + 'static,
T: 'static,
fn into_box(self) -> BoxReadonlyConsumer<T>where
Self: Sized + 'static,
T: 'static,
Convert to BoxReadonlyConsumer
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
§Returns
Returns the wrapped BoxReadonlyConsumer<T>
Sourcefn into_rc(self) -> RcReadonlyConsumer<T>where
Self: Sized + 'static,
T: 'static,
fn into_rc(self) -> RcReadonlyConsumer<T>where
Self: Sized + 'static,
T: 'static,
Convert to RcReadonlyConsumer
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
§Returns
Returns the wrapped RcReadonlyConsumer<T>
Sourcefn into_arc(self) -> ArcReadonlyConsumer<T>
fn into_arc(self) -> ArcReadonlyConsumer<T>
Convert to ArcReadonlyConsumer
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
§Returns
Returns the wrapped ArcReadonlyConsumer<T>
Sourcefn into_fn(self) -> impl Fn(&T)where
Self: Sized + 'static,
T: 'static,
fn into_fn(self) -> impl Fn(&T)where
Self: Sized + 'static,
T: 'static,
Convert to closure
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
Converts a readonly consumer to a closure that can be used directly in places where the standard library requires Fn.
§Returns
Returns a closure implementing Fn(&T)
§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};
let consumer = BoxReadonlyConsumer::new(|x: &i32| {
println!("Value: {}", x);
});
let func = consumer.into_fn();
func(&5);Implementors§
impl<T> ReadonlyConsumer<T> for ArcReadonlyConsumer<T>
impl<T> ReadonlyConsumer<T> for BoxReadonlyConsumer<T>
impl<T> ReadonlyConsumer<T> for RcReadonlyConsumer<T>
impl<T, F> ReadonlyConsumer<T> for F
Implement ReadonlyConsumer for all Fn(&T)