qubit_function/predicates/stateful_bi_predicate/
arc_stateful_bi_predicate.rs1use std::ops::Not;
14
15use super::{
16 ALWAYS_FALSE_NAME,
17 ALWAYS_TRUE_NAME,
18 Arc,
19 BoxStatefulBiPredicate,
20 Mutex,
21 RcStatefulBiPredicate,
22 StatefulBiPredicate,
23 impl_arc_conversions,
24 impl_closure_trait,
25 impl_predicate_clone,
26 impl_predicate_common_methods,
27 impl_predicate_debug_display,
28};
29
30type ArcStatefulBiPredicateFn<T, U> = Arc<Mutex<dyn FnMut(&T, &U) -> bool + Send + 'static>>;
31
32pub struct ArcStatefulBiPredicate<T, U> {
37 pub(super) function: ArcStatefulBiPredicateFn<T, U>,
38 pub(super) name: Option<String>,
39}
40
41impl<T, U> ArcStatefulBiPredicate<T, U> {
42 impl_predicate_common_methods!(
44 ArcStatefulBiPredicate<T, U>,
45 (FnMut(&T, &U) -> bool + Send + 'static),
46 |f| Arc::new(Mutex::new(f))
47 );
48
49 #[inline]
62 pub fn and<P>(&self, mut other: P) -> ArcStatefulBiPredicate<T, U>
63 where
64 P: StatefulBiPredicate<T, U> + Send + 'static,
65 T: 'static,
66 U: 'static,
67 {
68 let self_fn = self.function.clone();
69 ArcStatefulBiPredicate::new(move |first, second| {
70 let matched = (self_fn.lock())(first, second);
71 matched && other.test(first, second)
72 })
73 }
74
75 #[inline]
88 pub fn or<P>(&self, mut other: P) -> ArcStatefulBiPredicate<T, U>
89 where
90 P: StatefulBiPredicate<T, U> + Send + 'static,
91 T: 'static,
92 U: 'static,
93 {
94 let self_fn = self.function.clone();
95 ArcStatefulBiPredicate::new(move |first, second| {
96 let matched = (self_fn.lock())(first, second);
97 matched || other.test(first, second)
98 })
99 }
100
101 #[inline]
113 pub fn nand<P>(&self, mut other: P) -> ArcStatefulBiPredicate<T, U>
114 where
115 P: StatefulBiPredicate<T, U> + Send + 'static,
116 T: 'static,
117 U: 'static,
118 {
119 let self_fn = self.function.clone();
120 ArcStatefulBiPredicate::new(move |first, second| {
121 let matched = (self_fn.lock())(first, second);
122 !(matched && other.test(first, second))
123 })
124 }
125
126 #[inline]
139 pub fn xor<P>(&self, mut other: P) -> ArcStatefulBiPredicate<T, U>
140 where
141 P: StatefulBiPredicate<T, U> + Send + 'static,
142 T: 'static,
143 U: 'static,
144 {
145 let self_fn = self.function.clone();
146 ArcStatefulBiPredicate::new(move |first, second| {
147 let matched = (self_fn.lock())(first, second);
148 matched ^ other.test(first, second)
149 })
150 }
151
152 #[inline]
164 pub fn nor<P>(&self, mut other: P) -> ArcStatefulBiPredicate<T, U>
165 where
166 P: StatefulBiPredicate<T, U> + Send + 'static,
167 T: 'static,
168 U: 'static,
169 {
170 let self_fn = self.function.clone();
171 ArcStatefulBiPredicate::new(move |first, second| {
172 let matched = (self_fn.lock())(first, second);
173 !(matched || other.test(first, second))
174 })
175 }
176}
177
178impl<T, U> Not for ArcStatefulBiPredicate<T, U>
179where
180 T: 'static,
181 U: 'static,
182{
183 type Output = ArcStatefulBiPredicate<T, U>;
184
185 fn not(self) -> Self::Output {
186 let function = self.function;
187 ArcStatefulBiPredicate::new(move |first, second| !((function.lock())(first, second)))
188 }
189}
190
191impl<T, U> Not for &ArcStatefulBiPredicate<T, U>
192where
193 T: 'static,
194 U: 'static,
195{
196 type Output = ArcStatefulBiPredicate<T, U>;
197
198 fn not(self) -> Self::Output {
199 let function = self.function.clone();
200 ArcStatefulBiPredicate::new(move |first, second| !((function.lock())(first, second)))
201 }
202}
203
204impl_predicate_clone!(ArcStatefulBiPredicate<T, U>);
206
207impl_predicate_debug_display!(ArcStatefulBiPredicate<T, U>);
209
210impl<T, U> StatefulBiPredicate<T, U> for ArcStatefulBiPredicate<T, U> {
211 fn test(&mut self, first: &T, second: &U) -> bool {
212 (self.function.lock())(first, second)
213 }
214
215 impl_arc_conversions!(
217 ArcStatefulBiPredicate<T, U>,
218 BoxStatefulBiPredicate,
219 RcStatefulBiPredicate,
220 FnMut(first: &T, second: &U) -> bool
221 );
222}
223
224impl_closure_trait!(
226 StatefulBiPredicate<T, U>,
227 test,
228 FnMut(first: &T, second: &U) -> bool
229);