use std::ops::Not;
use {
super::ALWAYS_FALSE_NAME,
super::ALWAYS_TRUE_NAME,
crate::Predicate,
crate::predicates::macros::impl_predicate_clone,
crate::predicates::macros::impl_predicate_common_methods,
crate::predicates::macros::impl_predicate_debug_display,
crate::predicates::macros::impl_shared_predicate_methods,
std::sync::Arc,
};
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct ArcPredicate<T> {
pub(super) function: Arc<dyn Fn(&T) -> bool + Send + Sync>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T> ArcPredicate<T> {
impl_predicate_common_methods!(
ArcPredicate<T>,
semantic(Predicate<T> + Send + Sync + 'static),
|predicate| move |value: &T| predicate.test(value),
|f| Arc::new(f)
);
impl_shared_predicate_methods!(ArcPredicate<T>, Send + Sync + 'static);
}
impl<T> Not for ArcPredicate<T>
where
T: 'static,
{
type Output = ArcPredicate<T>;
fn not(self) -> Self::Output {
let metadata = self.metadata;
let function = self.function;
ArcPredicate::new_with_metadata(
move |value: &T| !function(value),
metadata,
)
}
}
impl<T> Not for &ArcPredicate<T>
where
T: 'static,
{
type Output = ArcPredicate<T>;
fn not(self) -> Self::Output {
let function = self.function.clone();
ArcPredicate::new_with_metadata(
move |value: &T| !function(value),
self.metadata.clone(),
)
}
}
impl_predicate_clone!(ArcPredicate<T>);
impl_predicate_debug_display!(ArcPredicate<T>);
impl<T> Predicate<T> for ArcPredicate<T> {
#[inline(always)]
fn test(&self, value: &T) -> bool {
(self.function)(value)
}
}