use std::ops::Not;
use std::sync::Arc;
use super::ALWAYS_FALSE_NAME;
use super::ALWAYS_TRUE_NAME;
use crate::Predicate;
use crate::predicates::macros::impl_predicate_clone;
use crate::predicates::macros::impl_predicate_common_methods;
use crate::predicates::macros::impl_predicate_debug_display;
use crate::predicates::macros::impl_shared_predicate_methods;
#[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)
}
}