Skip to main content

qubit_function/mutators/mutator/
box_conditional_mutator.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxConditionalMutator` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// 8. BoxConditionalMutator - Box-based Conditional Mutator
17// ============================================================================
18
19/// BoxConditionalMutator struct
20///
21/// A conditional mutator that only executes when a predicate is satisfied.
22/// Uses `BoxMutator` and `BoxPredicate` for single ownership semantics.
23///
24/// This type is typically created by calling `BoxMutator::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 mutates when predicate returns `true`
31/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
32/// - **Implements Mutator**: Can be used anywhere a `Mutator` is expected
33///
34/// # Examples
35///
36/// ## Basic Conditional Execution
37///
38/// ```rust
39/// use qubit_function::{Mutator, BoxMutator};
40///
41/// let mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
42/// let mut conditional = mutator.when(|x: &i32| *x > 0);
43///
44/// let mut positive = 5;
45/// conditional.apply(&mut positive);
46/// assert_eq!(positive, 10); // Executed
47///
48/// let mut negative = -5;
49/// conditional.apply(&mut negative);
50/// assert_eq!(negative, -5); // Not executed
51/// ```
52///
53/// ## With or_else Branch
54///
55/// ```rust
56/// use qubit_function::{Mutator, BoxMutator};
57///
58/// let mut mutator = BoxMutator::new(|x: &mut i32| *x *= 2)
59///     .when(|x: &i32| *x > 0)
60///     .or_else(|x: &mut i32| *x -= 1);
61///
62/// let mut positive = 5;
63/// mutator.apply(&mut positive);
64/// assert_eq!(positive, 10); // when branch executed
65///
66/// let mut negative = -5;
67/// mutator.apply(&mut negative);
68/// assert_eq!(negative, -6); // or_else branch executed
69/// ```
70///
71/// # Author
72///
73/// Haixing Hu
74pub struct BoxConditionalMutator<T> {
75    pub(super) mutator: BoxMutator<T>,
76    pub(super) predicate: BoxPredicate<T>,
77}
78
79// Generate box conditional mutator methods (and_then, or_else)
80impl_box_conditional_mutator!(BoxConditionalMutator<T>, BoxMutator, Mutator);
81
82impl<T> Mutator<T> for BoxConditionalMutator<T> {
83    fn apply(&self, value: &mut T) {
84        if self.predicate.test(value) {
85            self.mutator.apply(value);
86        }
87    }
88
89    // Generates: into_box(), into_rc(), into_fn()
90    impl_conditional_mutator_conversions!(BoxMutator<T>, RcMutator, Fn);
91}
92
93// Generate Debug and Display trait implementations for conditional mutator
94impl_conditional_mutator_debug_display!(BoxConditionalMutator<T>);