Skip to main content

qubit_function/predicates/bi_predicate/
box_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 `BoxBiPredicate` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15/// A Box-based bi-predicate with single ownership.
16///
17/// This type is suitable for one-time use scenarios where the
18/// bi-predicate does not need to be cloned or shared. Composition
19/// methods consume `self`, reflecting the single-ownership model.
20///
21/// # Examples
22///
23/// ```rust
24/// use qubit_function::{BiPredicate, BoxBiPredicate};
25///
26/// let pred = BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
27/// assert!(pred.test(&5, &3));
28///
29/// // Chaining consumes the bi-predicate
30/// let combined = pred.and(BoxBiPredicate::new(|x, y| x > y));
31/// assert!(combined.test(&10, &5));
32/// ```
33///
34/// # Author
35///
36/// Haixing Hu
37pub struct BoxBiPredicate<T, U> {
38    pub(super) function: Box<BiPredicateFn<T, U>>,
39    pub(super) name: Option<String>,
40}
41
42impl<T, U> BoxBiPredicate<T, U> {
43    // Generates: new(), new_with_name(), name(), set_name(), always_true(), always_false()
44    impl_predicate_common_methods!(
45        BoxBiPredicate<T, U>,
46        (Fn(&T, &U) -> bool + 'static),
47        |f| Box::new(f)
48    );
49
50    // Generates: and(), or(), not(), nand(), xor(), nor()
51    impl_box_predicate_methods!(BoxBiPredicate<T, U>);
52}
53
54// Generates: impl Debug for BoxBiPredicate<T, U> and impl Display for BoxBiPredicate<T, U>
55impl_predicate_debug_display!(BoxBiPredicate<T, U>);
56
57impl<T, U> BiPredicate<T, U> for BoxBiPredicate<T, U> {
58    fn test(&self, first: &T, second: &U) -> bool {
59        (self.function)(first, second)
60    }
61
62    // Generates: into_box(), into_rc(), into_fn()
63    impl_box_conversions!(
64        BoxBiPredicate<T, U>,
65        RcBiPredicate,
66        Fn(&T, &U) -> bool
67    );
68}