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