qubit_function/predicates/bi_predicate/rc_bi_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 `RcBiPredicate` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17/// An Rc-based bi-predicate with single-threaded shared ownership.
18///
19/// This type is suitable for scenarios where the bi-predicate needs
20/// to be reused in a single-threaded context. Composition methods
21/// borrow `&self`, allowing the original bi-predicate to remain
22/// usable after composition.
23///
24/// # Examples
25///
26/// ```rust
27/// use qubit_function::{BiPredicate, RcBiPredicate};
28///
29/// let pred = RcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
30/// assert!(pred.test(&5, &3));
31///
32/// // Original bi-predicate remains usable after composition
33/// let combined = pred.and(RcBiPredicate::new(|x, y| x > y));
34/// assert!(pred.test(&5, &3)); // Still works
35/// ```
36///
37pub struct RcBiPredicate<T, U> {
38 pub(super) function: Rc<BiPredicateFn<T, U>>,
39 pub(super) name: Option<String>,
40}
41
42impl<T, U> RcBiPredicate<T, U> {
43 // Generates: new(), new_with_name(), name(), set_name(), always_true(), always_false()
44 impl_predicate_common_methods!(
45 RcBiPredicate<T, U>,
46 (Fn(&T, &U) -> bool + 'static),
47 |f| Rc::new(f)
48 );
49
50 // Generates: and(), or(), not(), nand(), xor(), nor()
51 impl_shared_predicate_methods!(RcBiPredicate<T, U>, 'static);
52}
53
54// Generates: impl Clone for RcBiPredicate<T, U>
55impl_predicate_clone!(RcBiPredicate<T, U>);
56
57// Generates: impl Debug for RcBiPredicate<T, U> and impl Display for RcBiPredicate<T, U>
58impl_predicate_debug_display!(RcBiPredicate<T, U>);
59
60// Implements BiPredicate trait for RcBiPredicate<T, U>
61impl<T, U> BiPredicate<T, U> for RcBiPredicate<T, U> {
62 fn test(&self, first: &T, second: &U) -> bool {
63 (self.function)(first, second)
64 }
65
66 // Generates: into_box(), into_rc(), into_fn(), to_box(), to_rc(), to_fn()
67 impl_rc_conversions!(
68 RcBiPredicate<T, U>,
69 BoxBiPredicate,
70 Fn(first: &T, second: &U) -> bool
71 );
72}