qubit_function/consumers/bi_consumer_once/box_conditional_bi_consumer_once.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxConditionalBiConsumerOnce` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// =======================================================================
16// 5. BoxConditionalBiConsumerOnce - Box-based Conditional BiConsumerOnce
17// =======================================================================
18
19/// BoxConditionalBiConsumerOnce struct
20///
21/// A conditional one-time bi-consumer that only executes when a predicate is satisfied.
22/// Uses `BoxBiConsumerOnce` and `BoxBiPredicate` for single ownership semantics.
23///
24/// This type is typically created by calling `BoxBiConsumerOnce::when()` and is
25/// designed to work with the `or_else()` method to create if-then-else logic.
26///
27/// # Features
28///
29/// - **Single Ownership**: Not cloneable, consumes `self` on use
30/// - **Conditional Execution**: Only consumes when predicate returns `true`
31/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
32/// - **Implements BiConsumerOnce**: Can be used anywhere a `BiConsumerOnce` is expected
33///
34/// # Examples
35///
36/// ## Basic Conditional Execution
37///
38/// ```rust
39/// use qubit_function::{BiConsumerOnce, BoxBiConsumerOnce};
40/// use std::sync::{Arc, Mutex};
41///
42/// let log = Arc::new(Mutex::new(Vec::new()));
43/// let l = log.clone();
44/// let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
45/// l.lock().unwrap().push(*x + *y);
46/// });
47/// let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
48///
49/// conditional.accept(&5, &3);
50/// assert_eq!(*log.lock().unwrap(), vec![8]); // Executed
51/// ```
52///
53/// ## With or_else Branch
54///
55/// ```rust
56/// use qubit_function::{BiConsumerOnce, BoxBiConsumerOnce};
57/// use std::sync::{Arc, Mutex};
58///
59/// let log = Arc::new(Mutex::new(Vec::new()));
60/// let l1 = log.clone();
61/// let l2 = log.clone();
62/// let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
63/// l1.lock().unwrap().push(*x + *y);
64/// }).when(|x: &i32, y: &i32| *x > 0 && *y > 0)
65/// .or_else(move |x: &i32, y: &i32| {
66/// l2.lock().unwrap().push(*x * *y);
67/// });
68///
69/// consumer.accept(&5, &3);
70/// assert_eq!(*log.lock().unwrap(), vec![8]); // when branch executed
71/// ```
72///
73/// # Author
74///
75/// Haixing Hu
76pub struct BoxConditionalBiConsumerOnce<T, U> {
77 pub(super) consumer: BoxBiConsumerOnce<T, U>,
78 pub(super) predicate: BoxBiPredicate<T, U>,
79}
80
81// Generate and_then and or_else methods using macro
82impl_box_conditional_consumer!(
83 BoxConditionalBiConsumerOnce<T, U>,
84 BoxBiConsumerOnce,
85 BiConsumerOnce
86);
87
88impl<T, U> BiConsumerOnce<T, U> for BoxConditionalBiConsumerOnce<T, U> {
89 fn accept(self, first: &T, second: &U) {
90 if self.predicate.test(first, second) {
91 self.consumer.accept(first, second);
92 }
93 }
94
95 fn into_fn(self) -> impl FnOnce(&T, &U) {
96 let pred = self.predicate;
97 let consumer = self.consumer;
98 move |t: &T, u: &U| {
99 if pred.test(t, u) {
100 consumer.accept(t, u);
101 }
102 }
103 }
104}
105
106// Use macro to generate Debug and Display implementations
107impl_conditional_consumer_debug_display!(BoxConditionalBiConsumerOnce<T, U>);