use std::ops::Not;
use super::{
ALWAYS_FALSE_NAME,
ALWAYS_TRUE_NAME,
Predicate,
RcPredicate,
impl_box_conversions,
impl_box_predicate_methods,
impl_predicate_common_methods,
impl_predicate_debug_display,
};
pub struct BoxPredicate<T> {
pub(super) function: Box<dyn Fn(&T) -> bool>,
pub(super) name: Option<String>,
}
impl<T> BoxPredicate<T> {
impl_predicate_common_methods!(BoxPredicate<T>, (Fn(&T) -> bool + 'static), |f| Box::new(f));
impl_box_predicate_methods!(BoxPredicate<T>);
}
impl<T> Not for BoxPredicate<T>
where
T: 'static,
{
type Output = BoxPredicate<T>;
fn not(self) -> Self::Output {
BoxPredicate::new(move |value| !(self.function)(value))
}
}
impl_predicate_debug_display!(BoxPredicate<T>);
impl<T> Predicate<T> for BoxPredicate<T> {
fn test(&self, value: &T) -> bool {
(self.function)(value)
}
impl_box_conversions!(
BoxPredicate<T>,
RcPredicate,
Fn(&T) -> bool
);
}