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