pub trait FnBiConsumerOnceOps<T, U>: FnOnce(&T, &U) + Sized {
// Provided method
fn and_then<C>(self, next: C) -> BoxBiConsumerOnce<T, U>
where Self: 'static,
C: BiConsumerOnce<T, U> + 'static,
T: 'static,
U: 'static { ... }
}Expand description
Extension trait providing one-time bi-consumer composition methods for closures
Provides and_then and other composition methods for all closures
implementing FnOnce(&T, &U), enabling direct method chaining on
closures without explicit wrapper types.
§Features
- Natural Syntax: Chain operations directly on closures
- Returns BoxBiConsumerOnce: Composition results can be further chained
- Zero Cost: No overhead when composing closures
- Automatic Implementation: All
FnOnce(&T, &U)closures get these methods automatically
§Examples
use prism3_function::{BiConsumerOnce, FnBiConsumerOnceOps};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let 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§
Sourcefn and_then<C>(self, next: C) -> BoxBiConsumerOnce<T, U>where
Self: 'static,
C: BiConsumerOnce<T, U> + 'static,
T: 'static,
U: 'static,
fn and_then<C>(self, next: C) -> BoxBiConsumerOnce<T, U>where
Self: 'static,
C: BiConsumerOnce<T, U> + 'static,
T: 'static,
U: 'static,
Chains another one-time bi-consumer in sequence
Returns a new consumer executing the current operation first, then
the next operation. Consumes the current closure and returns
BoxBiConsumerOnce<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. SinceBoxBiConsumerOncecannot be cloned, the parameter will be consumed. Can be:- A closure:
|x: &T, y: &U| - A
BoxBiConsumerOnce<T, U> - Any type implementing
BiConsumerOnce<T, U>
- A closure:
§Returns
Returns the composed BoxBiConsumerOnce<T, U>
§Examples
use prism3_function::{BiConsumerOnce, FnBiConsumerOnceOps};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let 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);
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.