use std::ops::Not;
use {
super::ALWAYS_FALSE_NAME,
super::ALWAYS_TRUE_NAME,
crate::StatefulBiPredicate,
crate::predicates::macros::impl_predicate_common_methods,
crate::predicates::macros::impl_predicate_debug_display,
};
type BoxStatefulBiPredicateFn<T, U> = dyn FnMut(&T, &U) -> bool;
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct BoxStatefulBiPredicate<T, U> {
pub(super) function: Box<BoxStatefulBiPredicateFn<T, U>>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T, U> BoxStatefulBiPredicate<T, U> {
impl_predicate_common_methods!(
BoxStatefulBiPredicate<T, U>,
(FnMut(&T, &U) -> bool + 'static),
|f| Box::new(f)
);
#[inline]
pub fn and<P>(mut self, mut other: P) -> BoxStatefulBiPredicate<T, U>
where
P: StatefulBiPredicate<T, U> + 'static,
T: 'static,
U: 'static,
{
BoxStatefulBiPredicate::new(move |first: &T, second: &U| {
self.test(first, second) && other.test(first, second)
})
}
#[inline]
pub fn or<P>(mut self, mut other: P) -> BoxStatefulBiPredicate<T, U>
where
P: StatefulBiPredicate<T, U> + 'static,
T: 'static,
U: 'static,
{
BoxStatefulBiPredicate::new(move |first: &T, second: &U| {
self.test(first, second) || other.test(first, second)
})
}
#[inline]
pub fn nand<P>(mut self, mut other: P) -> BoxStatefulBiPredicate<T, U>
where
P: StatefulBiPredicate<T, U> + 'static,
T: 'static,
U: 'static,
{
BoxStatefulBiPredicate::new(move |first: &T, second: &U| {
!(self.test(first, second) && other.test(first, second))
})
}
#[inline]
pub fn xor<P>(mut self, mut other: P) -> BoxStatefulBiPredicate<T, U>
where
P: StatefulBiPredicate<T, U> + 'static,
T: 'static,
U: 'static,
{
BoxStatefulBiPredicate::new(move |first: &T, second: &U| {
self.test(first, second) ^ other.test(first, second)
})
}
#[inline]
pub fn nor<P>(mut self, mut other: P) -> BoxStatefulBiPredicate<T, U>
where
P: StatefulBiPredicate<T, U> + 'static,
T: 'static,
U: 'static,
{
BoxStatefulBiPredicate::new(move |first: &T, second: &U| {
!(self.test(first, second) || other.test(first, second))
})
}
}
impl<T, U> Not for BoxStatefulBiPredicate<T, U>
where
T: 'static,
U: 'static,
{
type Output = BoxStatefulBiPredicate<T, U>;
fn not(self) -> Self::Output {
let metadata = self.metadata;
let mut function = self.function;
BoxStatefulBiPredicate::new_with_metadata(
move |first: &T, second: &U| !function(first, second),
metadata,
)
}
}
impl_predicate_debug_display!(BoxStatefulBiPredicate<T, U>);
impl<T, U> StatefulBiPredicate<T, U> for BoxStatefulBiPredicate<T, U> {
#[inline(always)]
fn test(&mut self, first: &T, second: &U) -> bool {
(self.function)(first, second)
}
}