Skip to main content

always_predicate_demo/
always_predicate_demo.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9use qubit_function::{
10    ArcPredicate,
11    BoxPredicate,
12    Predicate,
13    RcPredicate,
14};
15
16fn main() {
17    println!("=== BoxPredicate always_true/always_false Demo ===\n");
18
19    // BoxPredicate::always_true
20    let always_true: BoxPredicate<i32> = BoxPredicate::always_true();
21    println!("BoxPredicate::always_true():");
22    println!("  test(&42): {}", always_true.test(&42));
23    println!("  test(&-1): {}", always_true.test(&-1));
24    println!("  test(&0): {}", always_true.test(&0));
25    println!("  name: {:?}", always_true.name());
26
27    // BoxPredicate::always_false
28    let always_false: BoxPredicate<i32> = BoxPredicate::always_false();
29    println!("\nBoxPredicate::always_false():");
30    println!("  test(&42): {}", always_false.test(&42));
31    println!("  test(&-1): {}", always_false.test(&-1));
32    println!("  test(&0): {}", always_false.test(&0));
33    println!("  name: {:?}", always_false.name());
34
35    println!("\n=== RcPredicate always_true/always_false Demo ===\n");
36
37    // RcPredicate::always_true
38    let rc_always_true: RcPredicate<String> = RcPredicate::always_true();
39    println!("RcPredicate::always_true():");
40    println!(
41        "  test(&\"hello\"): {}",
42        rc_always_true.test(&"hello".to_string())
43    );
44    println!(
45        "  test(&\"world\"): {}",
46        rc_always_true.test(&"world".to_string())
47    );
48    println!("  name: {:?}", rc_always_true.name());
49
50    // RcPredicate::always_false
51    let rc_always_false: RcPredicate<String> = RcPredicate::always_false();
52    println!("\nRcPredicate::always_false():");
53    println!(
54        "  test(&\"hello\"): {}",
55        rc_always_false.test(&"hello".to_string())
56    );
57    println!(
58        "  test(&\"world\"): {}",
59        rc_always_false.test(&"world".to_string())
60    );
61    println!("  name: {:?}", rc_always_false.name());
62
63    // Can be cloned and reused
64    let rc_clone = rc_always_true.clone();
65    println!("\nAfter cloning, still usable:");
66    println!(
67        "  Original: test(&\"test\"): {}",
68        rc_always_true.test(&"test".to_string())
69    );
70    println!(
71        "  Clone: test(&\"test\"): {}",
72        rc_clone.test(&"test".to_string())
73    );
74
75    println!("\n=== ArcPredicate always_true/always_false Demo ===\n");
76
77    // ArcPredicate::always_true
78    let arc_always_true: ArcPredicate<i32> = ArcPredicate::always_true();
79    println!("ArcPredicate::always_true():");
80    println!("  test(&100): {}", arc_always_true.test(&100));
81    println!("  test(&-100): {}", arc_always_true.test(&-100));
82    println!("  name: {:?}", arc_always_true.name());
83
84    // ArcPredicate::always_false
85    let arc_always_false: ArcPredicate<i32> = ArcPredicate::always_false();
86    println!("\nArcPredicate::always_false():");
87    println!("  test(&100): {}", arc_always_false.test(&100));
88    println!("  test(&-100): {}", arc_always_false.test(&-100));
89    println!("  name: {:?}", arc_always_false.name());
90
91    println!("\n=== Combining with other predicates ===\n");
92
93    // Combining with always_true (AND)
94    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
95    let combined_and_true = is_positive.and(BoxPredicate::always_true());
96    println!("is_positive AND always_true:");
97    println!(
98        "  test(&5): {} (equivalent to is_positive)",
99        combined_and_true.test(&5)
100    );
101    println!(
102        "  test(&-3): {} (equivalent to is_positive)",
103        combined_and_true.test(&-3)
104    );
105
106    // Combining with always_false (AND)
107    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
108    let combined_and_false = is_positive.and(BoxPredicate::always_false());
109    println!("\nis_positive AND always_false:");
110    println!("  test(&5): {} (always false)", combined_and_false.test(&5));
111    println!(
112        "  test(&-3): {} (always false)",
113        combined_and_false.test(&-3)
114    );
115
116    // Combining with always_true (OR)
117    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
118    let combined_or_true = is_positive.or(BoxPredicate::always_true());
119    println!("\nis_positive OR always_true:");
120    println!("  test(&5): {} (always true)", combined_or_true.test(&5));
121    println!("  test(&-3): {} (always true)", combined_or_true.test(&-3));
122
123    // Combining with always_false (OR)
124    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
125    let combined_or_false = is_positive.or(BoxPredicate::always_false());
126    println!("\nis_positive OR always_false:");
127    println!(
128        "  test(&5): {} (equivalent to is_positive)",
129        combined_or_false.test(&5)
130    );
131    println!(
132        "  test(&-3): {} (equivalent to is_positive)",
133        combined_or_false.test(&-3)
134    );
135
136    println!("\n=== Practical scenarios: Default pass/reject filters ===\n");
137
138    // Scenario 1: Default pass-all filter
139    let numbers = vec![1, 2, 3, 4, 5];
140    let pass_all = BoxPredicate::<i32>::always_true();
141    let filtered: Vec<_> = numbers.iter().copied().filter(pass_all.into_fn()).collect();
142    println!("Default pass all elements: {:?} -> {:?}", numbers, filtered);
143
144    // Scenario 2: Default reject-all filter
145    let numbers = vec![1, 2, 3, 4, 5];
146    let reject_all = BoxPredicate::<i32>::always_false();
147    let filtered: Vec<_> = numbers
148        .iter()
149        .copied()
150        .filter(reject_all.into_fn())
151        .collect();
152    println!(
153        "Default reject all elements: {:?} -> {:?}",
154        numbers, filtered
155    );
156
157    // Scenario 3: Configurable filter
158    fn configurable_filter(enable_filter: bool) -> BoxPredicate<i32> {
159        if enable_filter {
160            BoxPredicate::new(|x: &i32| *x > 3)
161        } else {
162            BoxPredicate::always_true()
163        }
164    }
165
166    let numbers = vec![1, 2, 3, 4, 5];
167
168    let filter_enabled = configurable_filter(true);
169    let filtered: Vec<_> = numbers
170        .iter()
171        .copied()
172        .filter(filter_enabled.into_fn())
173        .collect();
174    println!("\nFilter enabled: {:?} -> {:?}", numbers, filtered);
175
176    let filter_disabled = configurable_filter(false);
177    let filtered: Vec<_> = numbers
178        .iter()
179        .copied()
180        .filter(filter_disabled.into_fn())
181        .collect();
182    println!("Filter disabled: {:?} -> {:?}", numbers, filtered);
183}