Skip to main content

qubit_function/consumers/bi_consumer_once/
box_conditional_bi_consumer_once.rs

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