pub trait BiConsumer<T, U> {
// Required methods
fn accept(&mut self, first: &T, second: &U);
fn into_box(self) -> BoxBiConsumer<T, U>
where Self: Sized + 'static,
T: 'static,
U: 'static;
fn into_rc(self) -> RcBiConsumer<T, U>
where Self: Sized + 'static,
T: 'static,
U: 'static;
fn into_arc(self) -> ArcBiConsumer<T, U>
where Self: Sized + Send + 'static,
T: Send + 'static,
U: Send + 'static;
fn into_fn(self) -> impl FnMut(&T, &U)
where Self: Sized + 'static,
T: 'static,
U: 'static;
}Expand description
BiConsumer trait - Unified bi-consumer interface
Defines core behavior for all bi-consumer types. Similar to Java’s
BiConsumer<T, U> interface, performs operations accepting two values
but returning no result (side effects only).
BiConsumer can modify its own state (e.g., accumulate, count) but should NOT modify the consumed values themselves.
§Automatic Implementations
- All closures implementing
FnMut(&T, &U) BoxBiConsumer<T, U>,ArcBiConsumer<T, U>,RcBiConsumer<T, U>
§Features
- Unified Interface: All bi-consumer types share the same
acceptmethod signature - Automatic Implementation: Closures automatically implement this trait with zero overhead
- Type Conversions: Easy conversion between ownership models
- Generic Programming: Write functions accepting any bi-consumer type
§Examples
use prism3_function::{BiConsumer, BoxBiConsumer, ArcBiConsumer};
use std::sync::{Arc, Mutex};
fn apply_bi_consumer<C: BiConsumer<i32, i32>>(
consumer: &mut C,
a: &i32,
b: &i32
) {
consumer.accept(a, b);
}
// Works with any bi-consumer type
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let mut box_con = BoxBiConsumer::new(move |x: &i32, y: &i32| {
l.lock().unwrap().push(*x + *y);
});
apply_bi_consumer(&mut box_con, &5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);§Author
Haixing Hu
Required Methods§
Sourcefn accept(&mut self, first: &T, second: &U)
fn accept(&mut self, first: &T, second: &U)
Performs the consumption operation
Executes an operation on the given two references. The operation typically reads input values or produces side effects, but does not modify the input values themselves. Can modify the consumer’s own state.
§Parameters
first- Reference to the first value to consumesecond- Reference to the second value to consume
§Examples
use prism3_function::{BiConsumer, BoxBiConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let mut consumer = BoxBiConsumer::new(move |x: &i32, y: &i32| {
l.lock().unwrap().push(*x + *y);
});
consumer.accept(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);Sourcefn into_box(self) -> BoxBiConsumer<T, U>where
Self: Sized + 'static,
T: 'static,
U: 'static,
fn into_box(self) -> BoxBiConsumer<T, U>where
Self: Sized + 'static,
T: 'static,
U: 'static,
Converts to BoxBiConsumer
⚠️ Consumes self: Original consumer becomes unavailable after
calling this method.
Converts the current bi-consumer to BoxBiConsumer<T, U>.
§Ownership
This method consumes the consumer (takes ownership of self).
After calling, the original consumer is no longer available.
Tip: For cloneable consumers (ArcBiConsumer,
RcBiConsumer), call .clone() first if you need to keep the
original.
§Returns
Returns the wrapped BoxBiConsumer<T, U>
§Examples
use prism3_function::BiConsumer;
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32, y: &i32| {
l.lock().unwrap().push(*x + *y);
};
let mut box_consumer = closure.into_box();
box_consumer.accept(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);Sourcefn into_rc(self) -> RcBiConsumer<T, U>where
Self: Sized + 'static,
T: 'static,
U: 'static,
fn into_rc(self) -> RcBiConsumer<T, U>where
Self: Sized + 'static,
T: 'static,
U: 'static,
Converts to RcBiConsumer
⚠️ Consumes self: Original consumer becomes unavailable after
calling this method.
§Returns
Returns the wrapped RcBiConsumer<T, U>
Sourcefn into_arc(self) -> ArcBiConsumer<T, U>
fn into_arc(self) -> ArcBiConsumer<T, U>
Converts to ArcBiConsumer
⚠️ Consumes self: Original consumer becomes unavailable after
calling this method.
§Returns
Returns the wrapped ArcBiConsumer<T, U>
Sourcefn into_fn(self) -> impl FnMut(&T, &U)where
Self: Sized + 'static,
T: 'static,
U: 'static,
fn into_fn(self) -> impl FnMut(&T, &U)where
Self: Sized + 'static,
T: 'static,
U: 'static,
Converts bi-consumer to a closure
⚠️ Consumes self: Original consumer becomes unavailable after
calling this method.
Converts the bi-consumer to a closure usable with standard library
methods requiring FnMut.
§Returns
Returns a closure implementing FnMut(&T, &U)
§Examples
use prism3_function::{BiConsumer, BoxBiConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = BoxBiConsumer::new(move |x: &i32, y: &i32| {
l.lock().unwrap().push(*x + *y);
});
let mut func = consumer.into_fn();
func(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);Implementors§
impl<T, U> BiConsumer<T, U> for ArcBiConsumer<T, U>
impl<T, U> BiConsumer<T, U> for ArcConditionalBiConsumer<T, U>
impl<T, U> BiConsumer<T, U> for BoxBiConsumer<T, U>
impl<T, U> BiConsumer<T, U> for BoxConditionalBiConsumer<T, U>where
T: 'static,
U: 'static,
impl<T, U> BiConsumer<T, U> for RcBiConsumer<T, U>
impl<T, U> BiConsumer<T, U> for RcConditionalBiConsumer<T, U>where
T: 'static,
U: 'static,
impl<T, U, F> BiConsumer<T, U> for F
Implements BiConsumer for all FnMut(&T, &U)