FnBiConsumerOps

Trait FnBiConsumerOps 

Source
pub trait FnBiConsumerOps<T, U>: FnMut(&T, &U) + Sized {
    // Provided method
    fn and_then<C>(self, next: C) -> BoxBiConsumer<T, U>
       where Self: 'static,
             C: BiConsumer<T, U> + 'static,
             T: 'static,
             U: 'static { ... }
}
Expand description

Extension trait providing bi-consumer composition methods for closures

Provides and_then and other composition methods for all closures implementing FnMut(&T, &U), enabling direct method chaining on closures without explicit wrapper types.

§Design Rationale

This trait allows closures to be composed naturally using method syntax, similar to iterator combinators. Composition methods consume the closure and return BoxBiConsumer<T, U>, which can be further chained.

§Features

  • Natural Syntax: Chain operations directly on closures
  • Returns BoxBiConsumer: Composition results are BoxBiConsumer<T, U> for continued chaining
  • Zero Cost: No overhead when composing closures
  • Automatic Implementation: All FnMut(&T, &U) closures get these methods automatically

§Examples

use prism3_function::{BiConsumer, FnBiConsumerOps};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let mut chained = (move |x: &i32, y: &i32| {
    l1.lock().unwrap().push(*x + *y);
}).and_then(move |x: &i32, y: &i32| {
    l2.lock().unwrap().push(*x * *y);
});
chained.accept(&5, &3);
assert_eq!(*log.lock().unwrap(), vec![8, 15]);

§Author

Haixing Hu

Provided Methods§

Source

fn and_then<C>(self, next: C) -> BoxBiConsumer<T, U>
where Self: 'static, C: BiConsumer<T, U> + 'static, T: 'static, U: 'static,

Chains another consumer in sequence

Returns a new consumer executing the current operation first, then the next operation. Consumes the current closure and returns BoxBiConsumer<T, U>.

§Type Parameters
  • C - The type of the next consumer
§Parameters
  • next - The consumer to execute after the current operation. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original consumer, clone it first (if it implements Clone). Can be:
    • A closure: |x: &T, y: &U|
    • A BoxBiConsumer<T, U>
    • An ArcBiConsumer<T, U>
    • An RcBiConsumer<T, U>
    • Any type implementing BiConsumer<T, U>
§Returns

Returns the composed BoxBiConsumer<T, U>

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

let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let mut chained = (move |x: &i32, y: &i32| {
    l1.lock().unwrap().push(*x + *y);
}).and_then(move |x: &i32, y: &i32| {
    l2.lock().unwrap().push(*x * *y);
}).and_then(|x: &i32, y: &i32| println!("Result: {}, {}", x, y));

chained.accept(&5, &3); // Prints: Result: 5, 3
assert_eq!(*log.lock().unwrap(), vec![8, 15]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

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

Implements FnBiConsumerOps for all closure types