qubit_function/predicates/stateful_bi_predicate/
rc_stateful_bi_predicate.rs1use std::ops::Not;
14
15use super::{
16 ALWAYS_FALSE_NAME,
17 ALWAYS_TRUE_NAME,
18 BoxStatefulBiPredicate,
19 Rc,
20 RefCell,
21 StatefulBiPredicate,
22 impl_predicate_clone,
23 impl_predicate_common_methods,
24 impl_predicate_debug_display,
25 impl_rc_conversions,
26};
27
28type RcStatefulBiPredicateFn<T, U> = Rc<RefCell<dyn FnMut(&T, &U) -> bool>>;
29
30pub struct RcStatefulBiPredicate<T, U> {
35 pub(super) function: RcStatefulBiPredicateFn<T, U>,
36 pub(super) name: Option<String>,
37}
38
39impl<T, U> RcStatefulBiPredicate<T, U> {
40 impl_predicate_common_methods!(
42 RcStatefulBiPredicate<T, U>,
43 (FnMut(&T, &U) -> bool + 'static),
44 |f| Rc::new(RefCell::new(f))
45 );
46
47 #[inline]
60 pub fn and<P>(&self, mut other: P) -> RcStatefulBiPredicate<T, U>
61 where
62 P: StatefulBiPredicate<T, U> + 'static,
63 T: 'static,
64 U: 'static,
65 {
66 let self_fn = self.function.clone();
67 RcStatefulBiPredicate::new(move |first, second| {
68 let matched = (self_fn.borrow_mut())(first, second);
69 matched && other.test(first, second)
70 })
71 }
72
73 #[inline]
86 pub fn or<P>(&self, mut other: P) -> RcStatefulBiPredicate<T, U>
87 where
88 P: StatefulBiPredicate<T, U> + 'static,
89 T: 'static,
90 U: 'static,
91 {
92 let self_fn = self.function.clone();
93 RcStatefulBiPredicate::new(move |first, second| {
94 let matched = (self_fn.borrow_mut())(first, second);
95 matched || other.test(first, second)
96 })
97 }
98
99 #[inline]
111 pub fn nand<P>(&self, mut other: P) -> RcStatefulBiPredicate<T, U>
112 where
113 P: StatefulBiPredicate<T, U> + 'static,
114 T: 'static,
115 U: 'static,
116 {
117 let self_fn = self.function.clone();
118 RcStatefulBiPredicate::new(move |first, second| {
119 let matched = (self_fn.borrow_mut())(first, second);
120 !(matched && other.test(first, second))
121 })
122 }
123
124 #[inline]
137 pub fn xor<P>(&self, mut other: P) -> RcStatefulBiPredicate<T, U>
138 where
139 P: StatefulBiPredicate<T, U> + 'static,
140 T: 'static,
141 U: 'static,
142 {
143 let self_fn = self.function.clone();
144 RcStatefulBiPredicate::new(move |first, second| {
145 let matched = (self_fn.borrow_mut())(first, second);
146 matched ^ other.test(first, second)
147 })
148 }
149
150 #[inline]
162 pub fn nor<P>(&self, mut other: P) -> RcStatefulBiPredicate<T, U>
163 where
164 P: StatefulBiPredicate<T, U> + 'static,
165 T: 'static,
166 U: 'static,
167 {
168 let self_fn = self.function.clone();
169 RcStatefulBiPredicate::new(move |first, second| {
170 let matched = (self_fn.borrow_mut())(first, second);
171 !(matched || other.test(first, second))
172 })
173 }
174}
175
176impl<T, U> Not for RcStatefulBiPredicate<T, U>
177where
178 T: 'static,
179 U: 'static,
180{
181 type Output = RcStatefulBiPredicate<T, U>;
182
183 fn not(self) -> Self::Output {
184 let function = self.function;
185 RcStatefulBiPredicate::new(move |first, second| !((function.borrow_mut())(first, second)))
186 }
187}
188
189impl<T, U> Not for &RcStatefulBiPredicate<T, U>
190where
191 T: 'static,
192 U: 'static,
193{
194 type Output = RcStatefulBiPredicate<T, U>;
195
196 fn not(self) -> Self::Output {
197 let function = self.function.clone();
198 RcStatefulBiPredicate::new(move |first, second| !((function.borrow_mut())(first, second)))
199 }
200}
201
202impl_predicate_clone!(RcStatefulBiPredicate<T, U>);
204
205impl_predicate_debug_display!(RcStatefulBiPredicate<T, U>);
207
208impl<T, U> StatefulBiPredicate<T, U> for RcStatefulBiPredicate<T, U> {
209 fn test(&mut self, first: &T, second: &U) -> bool {
210 (self.function.borrow_mut())(first, second)
211 }
212
213 impl_rc_conversions!(
215 RcStatefulBiPredicate<T, U>,
216 BoxStatefulBiPredicate,
217 FnMut(first: &T, second: &U) -> bool
218 );
219}