use std::ops::Not;
use super::{
ALWAYS_FALSE_NAME,
ALWAYS_TRUE_NAME,
RcStatefulPredicate,
StatefulPredicate,
impl_box_conversions,
impl_predicate_common_methods,
impl_predicate_debug_display,
};
pub struct BoxStatefulPredicate<T> {
pub(super) function: Box<dyn FnMut(&T) -> bool>,
pub(super) name: Option<String>,
}
impl<T> BoxStatefulPredicate<T> {
impl_predicate_common_methods!(
BoxStatefulPredicate<T>,
(FnMut(&T) -> bool + 'static),
|f| Box::new(f)
);
#[inline]
pub fn and<P>(mut self, mut other: P) -> BoxStatefulPredicate<T>
where
P: StatefulPredicate<T> + 'static,
T: 'static,
{
BoxStatefulPredicate::new(move |value| self.test(value) && other.test(value))
}
#[inline]
pub fn or<P>(mut self, mut other: P) -> BoxStatefulPredicate<T>
where
P: StatefulPredicate<T> + 'static,
T: 'static,
{
BoxStatefulPredicate::new(move |value| self.test(value) || other.test(value))
}
#[inline]
pub fn nand<P>(mut self, mut other: P) -> BoxStatefulPredicate<T>
where
P: StatefulPredicate<T> + 'static,
T: 'static,
{
BoxStatefulPredicate::new(move |value| !(self.test(value) && other.test(value)))
}
#[inline]
pub fn xor<P>(mut self, mut other: P) -> BoxStatefulPredicate<T>
where
P: StatefulPredicate<T> + 'static,
T: 'static,
{
BoxStatefulPredicate::new(move |value| self.test(value) ^ other.test(value))
}
#[inline]
pub fn nor<P>(mut self, mut other: P) -> BoxStatefulPredicate<T>
where
P: StatefulPredicate<T> + 'static,
T: 'static,
{
BoxStatefulPredicate::new(move |value| !(self.test(value) || other.test(value)))
}
}
impl<T> Not for BoxStatefulPredicate<T>
where
T: 'static,
{
type Output = BoxStatefulPredicate<T>;
fn not(mut self) -> Self::Output {
BoxStatefulPredicate::new(move |value| !self.test(value))
}
}
impl_predicate_debug_display!(BoxStatefulPredicate<T>);
impl<T> StatefulPredicate<T> for BoxStatefulPredicate<T> {
fn test(&mut self, value: &T) -> bool {
(self.function)(value)
}
impl_box_conversions!(
BoxStatefulPredicate<T>,
RcStatefulPredicate,
FnMut(&T) -> bool
);
}