StatefulBiConsumer

Trait StatefulBiConsumer 

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

    // Provided methods
    fn into_box(self) -> BoxStatefulBiConsumer<T, U>
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn into_rc(self) -> RcStatefulBiConsumer<T, U>
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn into_arc(self) -> ArcStatefulBiConsumer<T, U>
       where Self: Sized + Send + 'static,
             T: 'static,
             U: 'static { ... }
    fn into_fn(self) -> impl FnMut(&T, &U)
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn into_once(self) -> BoxBiConsumerOnce<T, U>
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_box(&self) -> BoxStatefulBiConsumer<T, U>
       where Self: Sized + Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_rc(&self) -> RcStatefulBiConsumer<T, U>
       where Self: Sized + Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_arc(&self) -> ArcStatefulBiConsumer<T, U>
       where Self: Sized + Clone + Send + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_fn(&self) -> impl FnMut(&T, &U)
       where Self: Sized + Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_once(&self) -> BoxBiConsumerOnce<T, U>
       where Self: 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).

It is similar to the FnMut(&T, &U) trait in the standard library.

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)
  • BoxStatefulBiConsumer<T, U>, ArcStatefulBiConsumer<T, U>, RcStatefulBiConsumer<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, BoxStatefulBiConsumer, ArcStatefulBiConsumer};
use std::sync::{Arc, Mutex};

fn apply_bi_consumer<C: StatefulBiConsumer<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 = BoxStatefulBiConsumer::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, BoxStatefulBiConsumer};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let mut consumer = BoxStatefulBiConsumer::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) -> BoxStatefulBiConsumer<T, U>
where Self: Sized + 'static, T: 'static, U: 'static,

Converts to BoxStatefulBiConsumer

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

Converts the current bi-consumer to BoxStatefulBiConsumer<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 (ArcStatefulBiConsumer, RcStatefulBiConsumer), call .clone() first if you need to keep the original.

§Returns

Returns the wrapped BoxStatefulBiConsumer<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) -> RcStatefulBiConsumer<T, U>
where Self: Sized + 'static, T: 'static, U: 'static,

Converts to RcStatefulBiConsumer

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

§Returns

Returns the wrapped RcStatefulBiConsumer<T, U>

Source

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

Converts to ArcStatefulBiConsumer

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

§Returns

Returns the wrapped ArcStatefulBiConsumer<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, BoxStatefulBiConsumer};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = BoxStatefulBiConsumer::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 into_once(self) -> BoxBiConsumerOnce<T, U>
where Self: Sized + 'static, T: 'static, U: 'static,

Convert to BiConsumerOnce

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

Converts a reusable stateful bi-consumer to a one-time consumer that consumes itself on use. This enables passing StatefulBiConsumer to functions that require BiConsumerOnce.

§Returns

Returns a BoxBiConsumerOnce<T, U>

Source

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

Converts to BoxStatefulBiConsumer (non-consuming)

⚠️ Requires Clone: Original consumer must implement Clone.

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

§Ownership

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

§Returns

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

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

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = ArcStatefulBiConsumer::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) -> RcStatefulBiConsumer<T, U>
where Self: Sized + Clone + 'static, T: 'static, U: 'static,

Converts to RcStatefulBiConsumer (non-consuming)

⚠️ Requires Clone: Original consumer must implement Clone.

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

§Ownership

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

§Returns

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

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

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = ArcStatefulBiConsumer::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) -> ArcStatefulBiConsumer<T, U>
where Self: Sized + Clone + Send + 'static, T: 'static, U: 'static,

Converts to ArcStatefulBiConsumer (non-consuming)

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

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

§Ownership

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

§Returns

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

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

let log = Rc::new(RefCell::new(Vec::new()));
let l = log.clone();
let consumer = RcStatefulBiConsumer::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, BoxStatefulBiConsumer};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = BoxStatefulBiConsumer::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]);
Source

fn to_once(&self) -> BoxBiConsumerOnce<T, U>
where Self: Clone + 'static, T: 'static, U: 'static,

Convert to BiConsumerOnce 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 BoxBiConsumerOnce<T, U>

Implementors§

Source§

impl<T, U> StatefulBiConsumer<T, U> for ArcConditionalStatefulBiConsumer<T, U>
where T: Send + 'static, U: Send + 'static,

Source§

impl<T, U> StatefulBiConsumer<T, U> for ArcStatefulBiConsumer<T, U>

Source§

impl<T, U> StatefulBiConsumer<T, U> for BoxConditionalStatefulBiConsumer<T, U>
where T: 'static, U: 'static,

Source§

impl<T, U> StatefulBiConsumer<T, U> for BoxStatefulBiConsumer<T, U>

Source§

impl<T, U> StatefulBiConsumer<T, U> for RcConditionalStatefulBiConsumer<T, U>
where T: 'static, U: 'static,

Source§

impl<T, U> StatefulBiConsumer<T, U> for RcStatefulBiConsumer<T, U>

Source§

impl<T, U, F> StatefulBiConsumer<T, U> for F
where F: FnMut(&T, &U), T: 'static, U: 'static,