qubit_function/predicates/predicate/arc_predicate.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 `ArcPredicate` public type.
12
13use std::ops::Not;
14
15use super::{
16 ALWAYS_FALSE_NAME,
17 ALWAYS_TRUE_NAME,
18 Arc,
19 BoxPredicate,
20 Predicate,
21 RcPredicate,
22 impl_arc_conversions,
23 impl_closure_trait,
24 impl_predicate_clone,
25 impl_predicate_common_methods,
26 impl_predicate_debug_display,
27 impl_shared_predicate_methods,
28};
29
30/// An Arc-based predicate with thread-safe shared ownership.
31///
32/// This type is suitable for scenarios where the predicate needs to be
33/// shared across threads. Composition methods borrow `&self`, allowing the
34/// original predicate to remain usable after composition.
35///
36/// # Examples
37///
38/// ```rust
39/// use qubit_function::{Predicate, ArcPredicate};
40///
41/// let pred = ArcPredicate::new(|x: &i32| *x > 0);
42/// assert!(pred.test(&5));
43///
44/// // Original predicate remains usable after composition
45/// let combined = pred.and(ArcPredicate::new(|x| x % 2 == 0));
46/// assert!(pred.test(&5)); // Still works
47///
48/// // Can be cloned and sent across threads
49/// let pred_clone = pred.clone();
50/// std::thread::spawn(move || {
51/// assert!(pred_clone.test(&10));
52/// }).join().expect("thread should not panic");
53/// ```
54///
55pub struct ArcPredicate<T> {
56 pub(super) function: Arc<dyn Fn(&T) -> bool + Send + Sync>,
57 pub(super) name: Option<String>,
58}
59
60impl<T> ArcPredicate<T> {
61 // Generates: new(), new_with_name(), name(), set_name(), always_true(), always_false()
62 impl_predicate_common_methods!(ArcPredicate<T>, (Fn(&T) -> bool + Send + Sync + 'static), |f| Arc::new(
63 f
64 ));
65
66 // Generates: and(), or(), nand(), xor(), nor()
67 impl_shared_predicate_methods!(ArcPredicate<T>, Send + Sync + 'static);
68}
69
70impl<T> Not for ArcPredicate<T>
71where
72 T: 'static,
73{
74 type Output = ArcPredicate<T>;
75
76 fn not(self) -> Self::Output {
77 let function = self.function;
78 ArcPredicate::new(move |value| !function(value))
79 }
80}
81
82impl<T> Not for &ArcPredicate<T>
83where
84 T: 'static,
85{
86 type Output = ArcPredicate<T>;
87
88 fn not(self) -> Self::Output {
89 let function = self.function.clone();
90 ArcPredicate::new(move |value| !function(value))
91 }
92}
93
94// Generates: impl Clone for ArcPredicate<T>
95impl_predicate_clone!(ArcPredicate<T>);
96
97// Generates: impl Debug for ArcPredicate<T> and impl Display for ArcPredicate<T>
98impl_predicate_debug_display!(ArcPredicate<T>);
99
100// Implements Predicate trait for ArcPredicate<T>
101impl<T> Predicate<T> for ArcPredicate<T> {
102 fn test(&self, value: &T) -> bool {
103 (self.function)(value)
104 }
105
106 // Generates: into_box, into_rc, into_arc, into_fn, to_box, to_rc, to_arc, to_fn
107 impl_arc_conversions!(
108 ArcPredicate<T>,
109 BoxPredicate,
110 RcPredicate,
111 Fn(t: &T) -> bool
112 );
113}
114
115// Blanket implementation for all closures that match Fn(&T) -> bool
116impl_closure_trait!(
117 Predicate<T>,
118 test,
119 Fn(value: &T) -> bool
120);