Skip to main content

qubit_function/predicates/predicate/
rc_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 `RcPredicate` public type.
12
13use std::ops::Not;
14
15use super::{
16    ALWAYS_FALSE_NAME,
17    ALWAYS_TRUE_NAME,
18    BoxPredicate,
19    Predicate,
20    Rc,
21    impl_predicate_clone,
22    impl_predicate_common_methods,
23    impl_predicate_debug_display,
24    impl_rc_conversions,
25    impl_shared_predicate_methods,
26};
27
28/// An Rc-based predicate with single-threaded shared ownership.
29///
30/// This type is suitable for scenarios where the predicate needs to be
31/// reused in a single-threaded context. Composition methods borrow `&self`,
32/// allowing the original predicate to remain usable after composition.
33///
34/// # Examples
35///
36/// ```rust
37/// use qubit_function::{Predicate, RcPredicate};
38///
39/// let pred = RcPredicate::new(|x: &i32| *x > 0);
40/// assert!(pred.test(&5));
41///
42/// // Original predicate remains usable after composition
43/// let combined = pred.and(RcPredicate::new(|x| x % 2 == 0));
44/// assert!(pred.test(&5));  // Still works
45/// ```
46///
47pub struct RcPredicate<T> {
48    pub(super) function: Rc<dyn Fn(&T) -> bool>,
49    pub(super) name: Option<String>,
50}
51
52impl<T> RcPredicate<T> {
53    // Generates: new(), new_with_name(), name(), set_name(), always_true(), always_false()
54    impl_predicate_common_methods!(RcPredicate<T>, (Fn(&T) -> bool + 'static), |f| Rc::new(f));
55
56    // Generates: and(), or(), nand(), xor(), nor()
57    impl_shared_predicate_methods!(RcPredicate<T>, 'static);
58}
59
60impl<T> Not for RcPredicate<T>
61where
62    T: 'static,
63{
64    type Output = RcPredicate<T>;
65
66    fn not(self) -> Self::Output {
67        let function = self.function;
68        RcPredicate::new(move |value| !function(value))
69    }
70}
71
72impl<T> Not for &RcPredicate<T>
73where
74    T: 'static,
75{
76    type Output = RcPredicate<T>;
77
78    fn not(self) -> Self::Output {
79        let function = self.function.clone();
80        RcPredicate::new(move |value| !function(value))
81    }
82}
83
84// Generates: impl Clone for RcPredicate<T>
85impl_predicate_clone!(RcPredicate<T>);
86
87// Generates: impl Debug for RcPredicate<T> and impl Display for RcPredicate<T>
88impl_predicate_debug_display!(RcPredicate<T>);
89
90// Implements Predicate trait for RcPredicate<T>
91impl<T> Predicate<T> for RcPredicate<T> {
92    fn test(&self, value: &T) -> bool {
93        (self.function)(value)
94    }
95
96    // Generates: into_box(), into_rc(), into_fn(), to_box(), to_rc(), to_fn()
97    impl_rc_conversions!(
98        RcPredicate<T>,
99        BoxPredicate,
100        Fn(t: &T) -> bool
101    );
102}