pub trait BiConsumer<T, U> {
// Required method
fn accept(&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 + Sync + 'static,
T: 'static,
U: 'static { ... }
fn into_fn(self) -> impl Fn(&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) -> BoxBiConsumer<T, U>
where Self: Clone + 'static,
T: 'static,
U: 'static { ... }
fn to_rc(&self) -> RcBiConsumer<T, U>
where Self: Clone + 'static,
T: 'static,
U: 'static { ... }
fn to_arc(&self) -> ArcBiConsumer<T, U>
where Self: Clone + Send + Sync + 'static,
T: 'static,
U: 'static { ... }
fn to_fn(&self) -> impl Fn(&T, &U)
where Self: 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 readonly bi-consumer interface
It is similar to the Fn(&T, &U) trait in the standard library.
Defines core behavior for all readonly bi-consumer types. Unlike
BiConsumer, BiConsumer neither modifies its own state nor
the input values, making it a fully immutable operation.
§Automatic Implementations
- All closures implementing
Fn(&T, &U) BoxBiConsumer<T, U>,ArcBiConsumer<T, U>,RcBiConsumer<T, U>
§Features
- Unified Interface: All readonly 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 readonly bi-consumer type
- No Interior Mutability: No need for Mutex or RefCell, more efficient
§Examples
use prism3_function::{BiConsumer, BoxBiConsumer};
fn apply_consumer<C: BiConsumer<i32, i32>>(
consumer: &C,
a: &i32,
b: &i32
) {
consumer.accept(a, b);
}
let box_con = BoxBiConsumer::new(|x: &i32, y: &i32| {
println!("Sum: {}", x + y);
});
apply_consumer(&box_con, &5, &3);§Author
Haixing Hu
Required Methods§
Sourcefn accept(&self, first: &T, second: &U)
fn accept(&self, first: &T, second: &U)
Performs the readonly consumption operation
Executes an operation on the given two references. The operation typically reads input values or produces side effects, but neither modifies the input values nor 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};
let consumer = BoxBiConsumer::new(|x: &i32, y: &i32| {
println!("Values: {}, {}", x, y);
});
consumer.accept(&5, &3);Provided Methods§
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.
§Returns
Returns the wrapped BoxBiConsumer<T, U>
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 Fn(&T, &U)where
Self: Sized + 'static,
T: 'static,
U: 'static,
fn into_fn(self) -> impl Fn(&T, &U)where
Self: Sized + 'static,
T: 'static,
U: 'static,
Converts readonly bi-consumer to a closure
⚠️ Consumes self: Original consumer becomes unavailable after
calling this method.
Converts the readonly bi-consumer to a closure usable with standard
library methods requiring Fn.
§Returns
Returns a closure implementing Fn(&T, &U)
§Examples
use prism3_function::{BiConsumer, BoxBiConsumer};
let consumer = BoxBiConsumer::new(|x: &i32, y: &i32| {
println!("Sum: {}", x + y);
});
let func = consumer.into_fn();
func(&5, &3);Sourcefn into_once(self) -> BoxBiConsumerOnce<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,
Convert to BiConsumerOnce
⚠️ Consumes self: The original consumer will be unavailable after calling this method.
Converts a reusable readonly bi-consumer to a one-time consumer that consumes itself on use.
This enables passing BiConsumer to functions that require BiConsumerOnce.
§Returns
Returns a BoxBiConsumerOnce<T, U>
Sourcefn to_box(&self) -> BoxBiConsumer<T, U>where
Self: Clone + 'static,
T: 'static,
U: 'static,
fn to_box(&self) -> BoxBiConsumer<T, U>where
Self: Clone + 'static,
T: 'static,
U: 'static,
Converts to BoxBiConsumer (without consuming self)
Creates a new BoxBiConsumer by cloning the current consumer.
The original consumer remains usable after this call.
§Returns
Returns a new BoxBiConsumer<T, U>
§Examples
use prism3_function::{BiConsumer, RcBiConsumer};
let consumer = RcBiConsumer::new(|x: &i32, y: &i32| {
println!("Sum: {}", x + y);
});
let box_consumer = consumer.to_box();
box_consumer.accept(&5, &3);
// Original consumer still usable
consumer.accept(&10, &20);Sourcefn to_rc(&self) -> RcBiConsumer<T, U>where
Self: Clone + 'static,
T: 'static,
U: 'static,
fn to_rc(&self) -> RcBiConsumer<T, U>where
Self: Clone + 'static,
T: 'static,
U: 'static,
Converts to RcBiConsumer (without consuming self)
Creates a new RcBiConsumer by cloning the current consumer.
The original consumer remains usable after this call.
§Returns
Returns a new RcBiConsumer<T, U>
§Examples
use prism3_function::{BiConsumer, ArcBiConsumer};
let consumer = ArcBiConsumer::new(|x: &i32, y: &i32| {
println!("Sum: {}", x + y);
});
let rc_consumer = consumer.to_rc();
rc_consumer.accept(&5, &3);
// Original consumer still usable
consumer.accept(&10, &20);Sourcefn to_arc(&self) -> ArcBiConsumer<T, U>
fn to_arc(&self) -> ArcBiConsumer<T, U>
Converts to ArcBiConsumer (without consuming self)
Creates a new ArcBiConsumer by cloning the current consumer.
The original consumer remains usable after this call.
§Returns
Returns a new ArcBiConsumer<T, U>
§Examples
use prism3_function::{BiConsumer, RcBiConsumer};
let consumer = RcBiConsumer::new(|x: &i32, y: &i32| {
println!("Sum: {}", x + y);
});
// Note: This will only compile if the closure is Send + Sync
// For demonstration, we use a simple closure
let arc_consumer = consumer.to_arc();
arc_consumer.accept(&5, &3);Sourcefn to_fn(&self) -> impl Fn(&T, &U)where
Self: Clone + 'static,
T: 'static,
U: 'static,
fn to_fn(&self) -> impl Fn(&T, &U)where
Self: Clone + 'static,
T: 'static,
U: 'static,
Converts to a closure (without consuming self)
Creates a new closure by cloning the current consumer. The original consumer remains usable after this call.
§Returns
Returns a closure implementing Fn(&T, &U)
§Examples
use prism3_function::{BiConsumer, RcBiConsumer};
let consumer = RcBiConsumer::new(|x: &i32, y: &i32| {
println!("Sum: {}", x + y);
});
let func = consumer.to_fn();
func(&5, &3);
// Original consumer still usable
consumer.accept(&10, &20);Sourcefn to_once(&self) -> BoxBiConsumerOnce<T, U>where
Self: Clone + 'static,
T: 'static,
U: 'static,
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>