Skip to main content

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!(
63        ArcPredicate<T>,
64        (Fn(&T) -> bool + Send + Sync + 'static),
65        |f| Arc::new(f)
66    );
67
68    // Generates: and(), or(), nand(), xor(), nor()
69    impl_shared_predicate_methods!(ArcPredicate<T>, Send + Sync + 'static);
70}
71
72impl<T> Not for ArcPredicate<T>
73where
74    T: 'static,
75{
76    type Output = ArcPredicate<T>;
77
78    fn not(self) -> Self::Output {
79        let function = self.function;
80        ArcPredicate::new(move |value| !function(value))
81    }
82}
83
84impl<T> Not for &ArcPredicate<T>
85where
86    T: 'static,
87{
88    type Output = ArcPredicate<T>;
89
90    fn not(self) -> Self::Output {
91        let function = self.function.clone();
92        ArcPredicate::new(move |value| !function(value))
93    }
94}
95
96// Generates: impl Clone for ArcPredicate<T>
97impl_predicate_clone!(ArcPredicate<T>);
98
99// Generates: impl Debug for ArcPredicate<T> and impl Display for ArcPredicate<T>
100impl_predicate_debug_display!(ArcPredicate<T>);
101
102// Implements Predicate trait for ArcPredicate<T>
103impl<T> Predicate<T> for ArcPredicate<T> {
104    fn test(&self, value: &T) -> bool {
105        (self.function)(value)
106    }
107
108    // Generates: into_box, into_rc, into_arc, into_fn, to_box, to_rc, to_arc, to_fn
109    impl_arc_conversions!(
110        ArcPredicate<T>,
111        BoxPredicate,
112        RcPredicate,
113        Fn(t: &T) -> bool
114    );
115}
116
117// Blanket implementation for all closures that match Fn(&T) -> bool
118impl_closure_trait!(
119    Predicate<T>,
120    test,
121    Fn(value: &T) -> bool
122);