ReadonlyBiConsumer

Trait ReadonlyBiConsumer 

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

    // Provided methods
    fn into_box(self) -> BoxReadonlyBiConsumer<T, U>
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn into_rc(self) -> RcReadonlyBiConsumer<T, U>
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn into_arc(self) -> ArcReadonlyBiConsumer<T, U>
       where Self: Sized + Send + Sync + 'static,
             T: Send + Sync + 'static,
             U: Send + Sync + 'static { ... }
    fn into_fn(self) -> impl Fn(&T, &U)
       where Self: Sized + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_box(&self) -> BoxReadonlyBiConsumer<T, U>
       where Self: Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_rc(&self) -> RcReadonlyBiConsumer<T, U>
       where Self: Clone + 'static,
             T: 'static,
             U: 'static { ... }
    fn to_arc(&self) -> ArcReadonlyBiConsumer<T, U>
       where Self: Clone + Send + Sync + 'static,
             T: Send + Sync + 'static,
             U: Send + Sync + 'static { ... }
    fn to_fn(&self) -> impl Fn(&T, &U)
       where Self: Clone + 'static,
             T: 'static,
             U: 'static { ... }
}
Expand description

ReadonlyBiConsumer trait - Unified readonly bi-consumer interface

Defines core behavior for all readonly bi-consumer types. Unlike BiConsumer, ReadonlyBiConsumer neither modifies its own state nor the input values, making it a fully immutable operation.

§Automatic Implementations

  • All closures implementing Fn(&T, &U)
  • BoxReadonlyBiConsumer<T, U>, ArcReadonlyBiConsumer<T, U>, RcReadonlyBiConsumer<T, U>

§Features

  • Unified Interface: All readonly 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 readonly bi-consumer type
  • No Interior Mutability: No need for Mutex or RefCell, more efficient

§Examples

use prism3_function::{ReadonlyBiConsumer, BoxReadonlyBiConsumer};

fn apply_consumer<C: ReadonlyBiConsumer<i32, i32>>(
    consumer: &C,
    a: &i32,
    b: &i32
) {
    consumer.accept(a, b);
}

let box_con = BoxReadonlyBiConsumer::new(|x: &i32, y: &i32| {
    println!("Sum: {}", x + y);
});
apply_consumer(&box_con, &5, &3);

§Author

Haixing Hu

Required Methods§

Source

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 consume
  • second - Reference to the second value to consume
§Examples
use prism3_function::{ReadonlyBiConsumer, BoxReadonlyBiConsumer};

let consumer = BoxReadonlyBiConsumer::new(|x: &i32, y: &i32| {
    println!("Values: {}, {}", x, y);
});
consumer.accept(&5, &3);

Provided Methods§

Source

fn into_box(self) -> BoxReadonlyBiConsumer<T, U>
where Self: Sized + 'static, T: 'static, U: 'static,

Converts to BoxReadonlyBiConsumer

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

§Returns

Returns the wrapped BoxReadonlyBiConsumer<T, U>

Source

fn into_rc(self) -> RcReadonlyBiConsumer<T, U>
where Self: Sized + 'static, T: 'static, U: 'static,

Converts to RcReadonlyBiConsumer

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

§Returns

Returns the wrapped RcReadonlyBiConsumer<T, U>

Source

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

Converts to ArcReadonlyBiConsumer

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

§Returns

Returns the wrapped ArcReadonlyBiConsumer<T, U>

Source

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::{ReadonlyBiConsumer, BoxReadonlyBiConsumer};

let consumer = BoxReadonlyBiConsumer::new(|x: &i32, y: &i32| {
    println!("Sum: {}", x + y);
});
let func = consumer.into_fn();
func(&5, &3);
Source

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

Converts to BoxReadonlyBiConsumer (without consuming self)

Creates a new BoxReadonlyBiConsumer by cloning the current consumer. The original consumer remains usable after this call.

§Returns

Returns a new BoxReadonlyBiConsumer<T, U>

§Examples
use prism3_function::{ReadonlyBiConsumer, RcReadonlyBiConsumer};

let consumer = RcReadonlyBiConsumer::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);
Source

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

Converts to RcReadonlyBiConsumer (without consuming self)

Creates a new RcReadonlyBiConsumer by cloning the current consumer. The original consumer remains usable after this call.

§Returns

Returns a new RcReadonlyBiConsumer<T, U>

§Examples
use prism3_function::{ReadonlyBiConsumer, ArcReadonlyBiConsumer};

let consumer = ArcReadonlyBiConsumer::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);
Source

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

Converts to ArcReadonlyBiConsumer (without consuming self)

Creates a new ArcReadonlyBiConsumer by cloning the current consumer. The original consumer remains usable after this call.

§Returns

Returns a new ArcReadonlyBiConsumer<T, U>

§Examples
use prism3_function::{ReadonlyBiConsumer, RcReadonlyBiConsumer};

let consumer = RcReadonlyBiConsumer::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);
Source

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::{ReadonlyBiConsumer, RcReadonlyBiConsumer};

let consumer = RcReadonlyBiConsumer::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);

Implementors§

Source§

impl<T, U> ReadonlyBiConsumer<T, U> for ArcReadonlyBiConsumer<T, U>

Source§

impl<T, U> ReadonlyBiConsumer<T, U> for BoxReadonlyBiConsumer<T, U>

Source§

impl<T, U> ReadonlyBiConsumer<T, U> for RcReadonlyBiConsumer<T, U>

Source§

impl<T, U, F> ReadonlyBiConsumer<T, U> for F
where F: Fn(&T, &U),

Implements ReadonlyBiConsumer for all Fn(&T, &U)