BiConsumer

Trait BiConsumer 

Source
pub trait BiConsumer<T, U> {
    // Required method
    fn accept(&mut self, first: &T, second: &U);

    // Provided methods
    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 { ... }
    fn to_box(&self) -> BoxBiConsumer<T, U>
       where Self: Sized + Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_rc(&self) -> RcBiConsumer<T, U>
       where Self: Sized + Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_arc(&self) -> ArcBiConsumer<T, U>
       where Self: Sized + Clone + Send + 'static,
             T: Send + 'static,
             U: Send + 'static { ... }
    fn to_fn(&self) -> impl FnMut(&T, &U)
       where Self: Sized + Clone + '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 accept method 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§

Source

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 consume
  • second - 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]);

Provided Methods§

Source

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]);
Source

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>

Source

fn into_arc(self) -> ArcBiConsumer<T, U>
where Self: Sized + Send + 'static, T: Send + 'static, U: Send + 'static,

Converts to ArcBiConsumer

⚠️ Consumes self: Original consumer becomes unavailable after calling this method.

§Returns

Returns the wrapped ArcBiConsumer<T, U>

Source

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]);
Source

fn to_box(&self) -> BoxBiConsumer<T, U>
where Self: Sized + Clone + 'static, T: 'static, U: 'static,

Converts to BoxBiConsumer (non-consuming)

⚠️ Requires Clone: Original consumer must implement Clone.

Converts the current bi-consumer to BoxBiConsumer<T, U> by cloning it first.

§Ownership

This method does not consume the consumer. It clones the consumer and then converts the clone to BoxBiConsumer<T, U>. The original consumer remains available after calling this method.

§Returns

Returns the wrapped BoxBiConsumer<T, U> from the clone

§Examples
use prism3_function::{BiConsumer, ArcBiConsumer};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = ArcBiConsumer::new(move |x: &i32, y: &i32| {
    l.lock().unwrap().push(*x + *y);
});
let mut box_consumer = consumer.to_box();
box_consumer.accept(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);
// Original consumer still usable
consumer.accept(&2, &1);
assert_eq!(*log.lock().unwrap(), vec![8, 3]);
Source

fn to_rc(&self) -> RcBiConsumer<T, U>
where Self: Sized + Clone + 'static, T: 'static, U: 'static,

Converts to RcBiConsumer (non-consuming)

⚠️ Requires Clone: Original consumer must implement Clone.

Converts the current bi-consumer to RcBiConsumer<T, U> by cloning it first.

§Ownership

This method does not consume the consumer. It clones the consumer and then converts the clone to RcBiConsumer<T, U>. The original consumer remains available after calling this method.

§Returns

Returns the wrapped RcBiConsumer<T, U> from the clone

§Examples
use prism3_function::{BiConsumer, ArcBiConsumer};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = ArcBiConsumer::new(move |x: &i32, y: &i32| {
    l.lock().unwrap().push(*x + *y);
});
let mut rc_consumer = consumer.to_rc();
rc_consumer.accept(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);
// Original consumer still usable
consumer.accept(&2, &1);
assert_eq!(*log.lock().unwrap(), vec![8, 3]);
Source

fn to_arc(&self) -> ArcBiConsumer<T, U>
where Self: Sized + Clone + Send + 'static, T: Send + 'static, U: Send + 'static,

Converts to ArcBiConsumer (non-consuming)

⚠️ Requires Clone + Send: Original consumer must implement Clone + Send.

Converts the current bi-consumer to ArcBiConsumer<T, U> by cloning it first.

§Ownership

This method does not consume the consumer. It clones the consumer and then converts the clone to ArcBiConsumer<T, U>. The original consumer remains available after calling this method.

§Returns

Returns the wrapped ArcBiConsumer<T, U> from the clone

§Examples
use prism3_function::{BiConsumer, RcBiConsumer};
use std::rc::Rc;
use std::cell::RefCell;

let log = Rc::new(RefCell::new(Vec::new()));
let l = log.clone();
let consumer = RcBiConsumer::new(move |x: &i32, y: &i32| {
    l.borrow_mut().push(*x + *y);
});
let mut arc_consumer = consumer.to_arc();
arc_consumer.accept(&5, &3);
assert_eq!(*log.borrow(), vec![8]);
// Original consumer still usable
consumer.accept(&2, &1);
assert_eq!(*log.borrow(), vec![8, 3]);
Source

fn to_fn(&self) -> impl FnMut(&T, &U)
where Self: Sized + Clone + 'static, T: 'static, U: 'static,

Converts to closure (non-consuming)

⚠️ Requires Clone: 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.

§Returns

Returns a closure implementing FnMut(&T, &U) from the clone

§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.to_fn();
func(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8]);
// Original consumer still usable
consumer.accept(&2, &1);
assert_eq!(*log.lock().unwrap(), vec![8, 3]);

Implementors§

Source§

impl<T, U> BiConsumer<T, U> for ArcBiConsumer<T, U>

Source§

impl<T, U> BiConsumer<T, U> for ArcConditionalBiConsumer<T, U>
where T: Send + 'static, U: Send + 'static,

Source§

impl<T, U> BiConsumer<T, U> for BoxBiConsumer<T, U>

Source§

impl<T, U> BiConsumer<T, U> for BoxConditionalBiConsumer<T, U>
where T: 'static, U: 'static,

Source§

impl<T, U> BiConsumer<T, U> for RcBiConsumer<T, U>

Source§

impl<T, U> BiConsumer<T, U> for RcConditionalBiConsumer<T, U>
where T: 'static, U: 'static,

Source§

impl<T, U, F> BiConsumer<T, U> for F
where F: FnMut(&T, &U),

Implements BiConsumer for all FnMut(&T, &U)