use std::ops::Not;
use super::{
ALWAYS_FALSE_NAME,
ALWAYS_TRUE_NAME,
Arc,
BoxPredicate,
Predicate,
RcPredicate,
impl_arc_conversions,
impl_closure_trait,
impl_predicate_clone,
impl_predicate_common_methods,
impl_predicate_debug_display,
impl_shared_predicate_methods,
};
pub struct ArcPredicate<T> {
pub(super) function: Arc<dyn Fn(&T) -> bool + Send + Sync>,
pub(super) name: Option<String>,
}
impl<T> ArcPredicate<T> {
impl_predicate_common_methods!(
ArcPredicate<T>,
(Fn(&T) -> bool + Send + Sync + 'static),
|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 function = self.function;
ArcPredicate::new(move |value| !function(value))
}
}
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(move |value| !function(value))
}
}
impl_predicate_clone!(ArcPredicate<T>);
impl_predicate_debug_display!(ArcPredicate<T>);
impl<T> Predicate<T> for ArcPredicate<T> {
fn test(&self, value: &T) -> bool {
(self.function)(value)
}
impl_arc_conversions!(
ArcPredicate<T>,
BoxPredicate,
RcPredicate,
Fn(t: &T) -> bool
);
}
impl_closure_trait!(
Predicate<T>,
test,
Fn(value: &T) -> bool
);