Skip to main content

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