use std::ops::Not;
use {
super::ALWAYS_FALSE_NAME,
super::ALWAYS_TRUE_NAME,
crate::Predicate,
crate::predicates::macros::impl_box_predicate_methods,
crate::predicates::macros::impl_predicate_common_methods,
crate::predicates::macros::impl_predicate_debug_display,
};
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct BoxPredicate<T> {
pub(super) function: Box<dyn Fn(&T) -> bool>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T> BoxPredicate<T> {
impl_predicate_common_methods!(
BoxPredicate<T>,
semantic(Predicate<T> + 'static),
|predicate| move |value: &T| predicate.test(value),
|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 {
let metadata = self.metadata;
let function = self.function;
BoxPredicate::new_with_metadata(
move |value: &T| !function(value),
metadata,
)
}
}
impl_predicate_debug_display!(BoxPredicate<T>);
impl<T> Predicate<T> for BoxPredicate<T> {
#[inline(always)]
fn test(&self, value: &T) -> bool {
(self.function)(value)
}
}