pub trait Consumer<T> {
// Required method
fn accept(&self, value: &T);
// Provided methods
fn into_box(self) -> BoxConsumer<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_rc(self) -> RcConsumer<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_arc(self) -> ArcConsumer<T>
where Self: Sized + Send + Sync + 'static,
T: 'static { ... }
fn into_fn(self) -> impl Fn(&T)
where Self: Sized + 'static,
T: 'static { ... }
fn into_once(self) -> BoxConsumerOnce<T>
where Self: Sized + 'static,
T: 'static { ... }
fn to_box(&self) -> BoxConsumer<T>
where Self: Clone + 'static,
T: 'static { ... }
fn to_rc(&self) -> RcConsumer<T>
where Self: Clone + 'static,
T: 'static { ... }
fn to_arc(&self) -> ArcConsumer<T>
where Self: Clone + Send + Sync + 'static,
T: 'static { ... }
fn to_fn(&self) -> impl Fn(&T)
where Self: Clone + 'static,
T: 'static { ... }
fn to_once(&self) -> BoxConsumerOnce<T>
where Self: Clone + 'static,
T: 'static { ... }
}Expand description
Consumer trait - Unified readonly consumer interface
It is similar to the Fn(&T) trait in the standard library.
Defines the core behavior of all readonly consumer types. Unlike Consumer,
Consumer neither modifies its own state nor modifies input values,
making it a completely immutable operation.
§Auto-implementation
- All closures implementing
Fn(&T) BoxConsumer<T>,ArcConsumer<T>,RcConsumer<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::{Consumer, BoxConsumer};
fn apply_consumer<C: Consumer<i32>>(consumer: &C, value: &i32) {
consumer.accept(value);
}
let box_con = BoxConsumer::new(|x: &i32| {
println!("Value: {}", x);
});
apply_consumer(&box_con, &5);§Author
Haixing Hu
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::{Consumer, BoxConsumer};
let consumer = BoxConsumer::new(|x: &i32| println!("{}", x));
consumer.accept(&5);Provided Methods§
Sourcefn into_box(self) -> BoxConsumer<T>where
Self: Sized + 'static,
T: 'static,
fn into_box(self) -> BoxConsumer<T>where
Self: Sized + 'static,
T: 'static,
Convert to BoxConsumer
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
§Returns
Returns the wrapped BoxConsumer<T>
Sourcefn into_rc(self) -> RcConsumer<T>where
Self: Sized + 'static,
T: 'static,
fn into_rc(self) -> RcConsumer<T>where
Self: Sized + 'static,
T: 'static,
Convert to RcConsumer
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
§Returns
Returns the wrapped RcConsumer<T>
Sourcefn into_arc(self) -> ArcConsumer<T>
fn into_arc(self) -> ArcConsumer<T>
Convert to ArcConsumer
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
§Returns
Returns the wrapped ArcConsumer<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::{Consumer, BoxConsumer};
let consumer = BoxConsumer::new(|x: &i32| {
println!("Value: {}", x);
});
let func = consumer.into_fn();
func(&5);Sourcefn into_once(self) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
T: 'static,
fn into_once(self) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
T: 'static,
Convert to ConsumerOnce
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
Converts a reusable readonly consumer to a one-time consumer that consumes itself on use.
This enables passing Consumer to functions that require ConsumerOnce.
§Returns
Returns a BoxConsumerOnce<T>
§Examples
fn takes_once<C: ConsumerOnce<i32>>(consumer: C, value: &i32) {
consumer.accept(value);
}
let consumer = BoxConsumer::new(|x: &i32| println!("{}", x));
takes_once(consumer.into_once(), &5);Sourcefn to_box(&self) -> BoxConsumer<T>where
Self: Clone + 'static,
T: 'static,
fn to_box(&self) -> BoxConsumer<T>where
Self: Clone + 'static,
T: 'static,
Non-consuming conversion to BoxConsumer
⚠️ Does NOT consume self: This method clones self and returns a
boxed readonly consumer that calls the cloned consumer. Requires
Self: Clone so it can be called through an immutable reference.
§Returns
Returns the wrapped BoxConsumer<T>
Sourcefn to_rc(&self) -> RcConsumer<T>where
Self: Clone + 'static,
T: 'static,
fn to_rc(&self) -> RcConsumer<T>where
Self: Clone + 'static,
T: 'static,
Non-consuming conversion to RcConsumer
⚠️ Does NOT consume self: Clones self and returns an
RcConsumer that forwards to the cloned consumer. Requires
Self: Clone.
§Returns
Returns the wrapped RcConsumer<T>
Sourcefn to_arc(&self) -> ArcConsumer<T>
fn to_arc(&self) -> ArcConsumer<T>
Non-consuming conversion to ArcConsumer
⚠️ Does NOT consume self: Clones self and returns an
ArcConsumer. Requires Self: Clone + Send + Sync so the result
is thread-safe.
§Returns
Returns the wrapped ArcConsumer<T>
Sourcefn to_fn(&self) -> impl Fn(&T)where
Self: Clone + 'static,
T: 'static,
fn to_fn(&self) -> impl Fn(&T)where
Self: Clone + 'static,
T: 'static,
Non-consuming conversion to a boxed closure
⚠️ Does NOT consume self: Returns a closure which calls a cloned
copy of the consumer. Requires Self: Clone.
§Returns
Returns a closure implementing Fn(&T) which forwards to the cloned
consumer.
Sourcefn to_once(&self) -> BoxConsumerOnce<T>where
Self: Clone + 'static,
T: 'static,
fn to_once(&self) -> BoxConsumerOnce<T>where
Self: Clone + 'static,
T: 'static,
Convert to ConsumerOnce without consuming self
⚠️ Requires Clone: This method requires Self to implement Clone.
Clones the current consumer and converts the clone to a one-time consumer.
§Returns
Returns a BoxConsumerOnce<T>
§Examples
fn takes_once<C: ConsumerOnce<i32>>(consumer: C, value: &i32) {
consumer.accept(value);
}
let consumer = BoxConsumer::new(|x: &i32| println!("{}", x));
takes_once(consumer.to_once(), &5);