qubit_function/consumers/consumer_once/box_conditional_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 `BoxConditionalConsumerOnce` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// 5. BoxConditionalConsumerOnce - Box-based Conditional Consumer
17// ============================================================================
18
19/// BoxConditionalConsumerOnce struct
20///
21/// A conditional one-time consumer that only executes when a predicate is satisfied.
22/// Uses `BoxConsumerOnce` and `BoxPredicate` for single ownership semantics.
23///
24/// This type is typically created by calling `BoxConsumerOnce::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 ConsumerOnce**: Can be used anywhere a `ConsumerOnce` is expected
33///
34/// # Examples
35///
36/// ## Basic Conditional Execution
37///
38/// ```rust
39/// use qubit_function::{ConsumerOnce, BoxConsumerOnce};
40/// use std::sync::{Arc, Mutex};
41///
42/// let log = Arc::new(Mutex::new(Vec::new()));
43/// let l = log.clone();
44/// let consumer = BoxConsumerOnce::new(move |x: &i32| {
45/// l.lock().unwrap().push(*x);
46/// });
47/// let conditional = consumer.when(|x: &i32| *x > 0);
48///
49/// conditional.accept(&5);
50/// assert_eq!(*log.lock().unwrap(), vec![5]); // Executed
51/// ```
52///
53/// ## With or_else Branch
54///
55/// ```rust
56/// use qubit_function::{ConsumerOnce, BoxConsumerOnce};
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 = BoxConsumerOnce::new(move |x: &i32| {
63/// l1.lock().unwrap().push(*x);
64/// })
65/// .when(|x: &i32| *x > 0)
66/// .or_else(move |x: &i32| {
67/// l2.lock().unwrap().push(-*x);
68/// });
69///
70/// consumer.accept(&5);
71/// assert_eq!(*log.lock().unwrap(), vec![5]); // when branch executed
72/// ```
73///
74/// # Author
75///
76/// Haixing Hu
77pub struct BoxConditionalConsumerOnce<T> {
78 pub(super) consumer: BoxConsumerOnce<T>,
79 pub(super) predicate: BoxPredicate<T>,
80}
81
82// Generate and_then and or_else methods using macro
83impl_box_conditional_consumer!(BoxConditionalConsumerOnce<T>, BoxConsumerOnce, ConsumerOnce);
84
85impl<T> ConsumerOnce<T> for BoxConditionalConsumerOnce<T> {
86 fn accept(self, value: &T) {
87 if self.predicate.test(value) {
88 self.consumer.accept(value);
89 }
90 }
91
92 fn into_fn(self) -> impl FnOnce(&T) {
93 let pred = self.predicate;
94 let consumer = self.consumer;
95 move |t: &T| {
96 if pred.test(t) {
97 consumer.accept(t);
98 }
99 }
100 }
101
102 // do NOT override ConsumerOnce::to_xxxx() because BoxConditionalConsumerOnce is not Clone
103 // and calling BoxConditionalConsumerOnce::to_xxxx() will cause a compile error
104}
105
106// Use macro to generate Debug and Display implementations
107impl_conditional_consumer_debug_display!(BoxConditionalConsumerOnce<T>);