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