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