Skip to main content

qubit_function/predicates/bi_predicate/
arc_bi_predicate.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `ArcBiPredicate` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15/// An Arc-based bi-predicate with thread-safe shared ownership.
16///
17/// This type is suitable for scenarios where the bi-predicate needs
18/// to be shared across threads. Composition methods borrow `&self`,
19/// allowing the original bi-predicate to remain usable after
20/// composition.
21///
22/// # Examples
23///
24/// ```rust
25/// use qubit_function::{BiPredicate, ArcBiPredicate};
26///
27/// let pred = ArcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
28/// assert!(pred.test(&5, &3));
29///
30/// // Original bi-predicate remains usable after composition
31/// let combined = pred.and(ArcBiPredicate::new(|x, y| x > y));
32/// assert!(pred.test(&5, &3));  // Still works
33///
34/// // Can be cloned and sent across threads
35/// let pred_clone = pred.clone();
36/// std::thread::spawn(move || {
37///     assert!(pred_clone.test(&10, &5));
38/// }).join().unwrap();
39/// ```
40///
41/// # Author
42///
43/// Haixing Hu
44pub struct ArcBiPredicate<T, U> {
45    pub(super) function: Arc<SendSyncBiPredicateFn<T, U>>,
46    pub(super) name: Option<String>,
47}
48
49impl<T, U> ArcBiPredicate<T, U> {
50    // Generates: new(), new_with_name(), name(), set_name(), always_true(), always_false()
51    impl_predicate_common_methods!(
52        ArcBiPredicate<T, U>,
53        (Fn(&T, &U) -> bool + Send + Sync + 'static),
54        |f| Arc::new(f)
55    );
56
57    // Generates: and(), or(), not(), nand(), xor(), nor()
58    impl_shared_predicate_methods!(
59        ArcBiPredicate<T, U>,
60        Send + Sync + 'static
61    );
62}
63
64// Generates: impl Clone for ArcBiPredicate<T, U>
65impl_predicate_clone!(ArcBiPredicate<T, U>);
66
67// Generates: impl Debug for ArcBiPredicate<T, U> and impl Display for ArcBiPredicate<T, U>
68impl_predicate_debug_display!(ArcBiPredicate<T, U>);
69
70// Implements BiPredicate trait for ArcBiPredicate<T, U>
71impl<T, U> BiPredicate<T, U> for ArcBiPredicate<T, U> {
72    fn test(&self, first: &T, second: &U) -> bool {
73        (self.function)(first, second)
74    }
75
76    // Generates: into_box, into_rc, into_arc, into_fn, to_box, to_rc, to_arc, to_fn
77    impl_arc_conversions!(
78        ArcBiPredicate<T, U>,
79        BoxBiPredicate,
80        RcBiPredicate,
81        Fn(first: &T, second: &U) -> bool
82    );
83}
84
85// Blanket implementation for all closures that match
86// Fn(&T, &U) -> bool. This provides optimal implementations for
87// closures by wrapping them directly into the target type.
88impl_closure_trait!(
89    BiPredicate<T, U>,
90    test,
91    Fn(first: &T, second: &U) -> bool
92);