Skip to main content

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