pub trait StatefulConsumer<T> {
// Required method
fn accept(&mut self, value: &T);
// Provided methods
fn into_box(self) -> BoxStatefulConsumer<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_rc(self) -> RcStatefulConsumer<T>
where Self: Sized + 'static,
T: 'static { ... }
fn into_arc(self) -> ArcStatefulConsumer<T>
where Self: Sized + Send + 'static,
T: 'static { ... }
fn into_fn(self) -> impl FnMut(&T)
where Self: Sized + 'static,
T: 'static { ... }
fn into_once(self) -> BoxConsumerOnce<T>
where Self: Sized + 'static,
T: 'static { ... }
fn to_box(&self) -> BoxStatefulConsumer<T>
where Self: Sized + Clone + 'static,
T: 'static { ... }
fn to_rc(&self) -> RcStatefulConsumer<T>
where Self: Sized + Clone + 'static,
T: 'static { ... }
fn to_arc(&self) -> ArcStatefulConsumer<T>
where Self: Sized + Clone + Send + 'static,
T: 'static { ... }
fn to_fn(&self) -> impl FnMut(&T)
where Self: Sized + Clone + 'static,
T: 'static { ... }
fn to_once(&self) -> BoxConsumerOnce<T>
where Self: Clone + 'static,
T: 'static { ... }
}Expand description
Consumer trait - Unified consumer interface
Defines the core behavior of all consumer types. Similar to Java’s
Consumer<T> interface, executes operations that accept a value but return
no result (side effects only).
It is similar to the FnMut(&T) trait in the standard library.
Consumer can modify its own state (such as accumulation, counting), but should not modify the consumed value itself.
§Automatic Implementation
- All closures implementing
FnMut(&T) BoxStatefulConsumer<T>,ArcStatefulConsumer<T>,RcStatefulConsumer<T>
§Features
- Unified Interface: All consumer types share the same
acceptmethod signature - Automatic 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 consumer type
§Examples
use prism3_function::{Consumer, BoxStatefulConsumer, ArcStatefulConsumer};
use std::sync::{Arc, Mutex};
fn apply_consumer<C: StatefulConsumer<i32>>(consumer: &mut C, value: &i32) {
consumer.accept(value);
}
// Works with any consumer type
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let mut box_con = BoxStatefulConsumer::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
apply_consumer(&mut box_con, &5);
assert_eq!(*log.lock().unwrap(), vec![5]);§Author
Haixing Hu
Required Methods§
Sourcefn accept(&mut self, value: &T)
fn accept(&mut self, value: &T)
Execute consumption operation
Performs an operation on the given reference. The operation typically reads the input value or produces side effects, but does not modify the input value itself. Can modify the consumer’s own state.
§Parameters
value- Reference to the value to be consumed
§Examples
use prism3_function::{Consumer, BoxStatefulConsumer};
let mut consumer = BoxStatefulConsumer::new(|x: &i32| println!("{}", x));
let value = 5;
consumer.accept(&value);Provided Methods§
Sourcefn into_box(self) -> BoxStatefulConsumer<T>where
Self: Sized + 'static,
T: 'static,
fn into_box(self) -> BoxStatefulConsumer<T>where
Self: Sized + 'static,
T: 'static,
Convert to BoxStatefulConsumer
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
Converts the current consumer to BoxStatefulConsumer<T>.
§Ownership
This method consumes the consumer (takes ownership of self).
After calling this method, the original consumer is no longer available.
Tip: For cloneable consumers (ArcStatefulConsumer, RcStatefulConsumer),
if you need to preserve the original object, you can call .clone()
first.
§Return Value
Returns the wrapped BoxStatefulConsumer<T>
§Examples
use prism3_function::Consumer;
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
l.lock().unwrap().push(*x);
};
let mut box_consumer = closure.into_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);Sourcefn into_rc(self) -> RcStatefulConsumer<T>where
Self: Sized + 'static,
T: 'static,
fn into_rc(self) -> RcStatefulConsumer<T>where
Self: Sized + 'static,
T: 'static,
Convert to RcStatefulConsumer
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
§Return Value
Returns the wrapped RcStatefulConsumer<T>
Sourcefn into_arc(self) -> ArcStatefulConsumer<T>
fn into_arc(self) -> ArcStatefulConsumer<T>
Convert to ArcStatefulConsumer
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
§Return Value
Returns the wrapped ArcStatefulConsumer<T>
Sourcefn into_fn(self) -> impl FnMut(&T)where
Self: Sized + 'static,
T: 'static,
fn into_fn(self) -> impl FnMut(&T)where
Self: Sized + 'static,
T: 'static,
Convert to closure
⚠️ Consumes self: The original consumer will be unavailable after
calling this method.
Converts the consumer to a closure that can be used directly in standard
library functions requiring FnMut.
§Return Value
Returns a closure implementing FnMut(&T)
§Examples
use prism3_function::{Consumer, BoxStatefulConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = BoxStatefulConsumer::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
let mut func = consumer.into_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![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 stateful consumer to a one-time consumer that consumes itself on use.
This enables passing StatefulConsumer 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 = BoxStatefulConsumer::new(|x: &i32| println!("{}", x));
takes_once(consumer.into_once(), &5);Sourcefn to_box(&self) -> BoxStatefulConsumer<T>
fn to_box(&self) -> BoxStatefulConsumer<T>
Convert to BoxStatefulConsumer
⚠️ Requires Clone: The original consumer must implement Clone.
Converts the current consumer to BoxStatefulConsumer<T> by cloning it first.
§Ownership
This method does not consume the consumer. It clones the consumer and
then converts the clone to BoxStatefulConsumer<T>. The original consumer remains
available after calling this method.
§Return Value
Returns the wrapped BoxStatefulConsumer<T> from the clone
§Examples
use prism3_function::{Consumer, ArcStatefulConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = ArcStatefulConsumer::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
let mut box_consumer = consumer.to_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
// Original consumer still usable
consumer.accept(&3);
assert_eq!(*log.lock().unwrap(), vec![5, 3]);Sourcefn to_rc(&self) -> RcStatefulConsumer<T>
fn to_rc(&self) -> RcStatefulConsumer<T>
Convert to RcStatefulConsumer
⚠️ Requires Clone: The original consumer must implement Clone.
Converts the current consumer to RcStatefulConsumer<T> by cloning it first.
§Ownership
This method does not consume the consumer. It clones the consumer and
then converts the clone to RcStatefulConsumer<T>. The original consumer remains
available after calling this method.
§Return Value
Returns the wrapped RcStatefulConsumer<T> from the clone
§Examples
use prism3_function::{Consumer, ArcStatefulConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = ArcStatefulConsumer::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
let mut rc_consumer = consumer.to_rc();
rc_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
// Original consumer still usable
consumer.accept(&3);
assert_eq!(*log.lock().unwrap(), vec![5, 3]);Sourcefn to_arc(&self) -> ArcStatefulConsumer<T>
fn to_arc(&self) -> ArcStatefulConsumer<T>
Convert to ArcStatefulConsumer
⚠️ Requires Clone + Send: The original consumer must implement Clone + Send.
Converts the current consumer to ArcStatefulConsumer<T> by cloning it first.
§Ownership
This method does not consume the consumer. It clones the consumer and
then converts the clone to ArcStatefulConsumer<T>. The original consumer remains
available after calling this method.
§Return Value
Returns the wrapped ArcStatefulConsumer<T> from the clone
§Examples
use prism3_function::{Consumer, RcStatefulConsumer};
use std::rc::Rc;
use std::cell::RefCell;
let log = Rc::new(RefCell::new(Vec::new()));
let l = log.clone();
let consumer = RcStatefulConsumer::new(move |x: &i32| {
l.borrow_mut().push(*x);
});
let mut arc_consumer = consumer.to_arc();
arc_consumer.accept(&5);
assert_eq!(*log.borrow(), vec![5]);
// Original consumer still usable
consumer.accept(&3);
assert_eq!(*log.borrow(), vec![5, 3]);Sourcefn to_fn(&self) -> impl FnMut(&T)
fn to_fn(&self) -> impl FnMut(&T)
Convert to closure
⚠️ Requires Clone: The original consumer must implement Clone.
Converts the consumer to a closure that can be used directly in standard
library functions requiring FnMut.
§Ownership
This method does not consume the consumer. It clones the consumer and then converts the clone to a closure. The original consumer remains available after calling this method.
§Return Value
Returns a closure implementing FnMut(&T) from the clone
§Examples
use prism3_function::{Consumer, BoxStatefulConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = BoxStatefulConsumer::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
let mut func = consumer.to_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
// Original consumer still usable
consumer.accept(&3);
assert_eq!(*log.lock().unwrap(), vec![5, 3]);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 = BoxStatefulConsumer::new(|x: &i32| println!("{}", x));
takes_once(consumer.to_once(), &5);