use std::ops::Not;
use std::sync::Arc;
use parking_lot::Mutex;
use super::ALWAYS_FALSE_NAME;
use super::ALWAYS_TRUE_NAME;
use crate::StatefulPredicate;
use crate::predicates::macros::impl_predicate_clone;
use crate::predicates::macros::impl_predicate_common_methods;
use crate::predicates::macros::impl_predicate_debug_display;
type ArcStatefulPredicateFn<T> = Arc<Mutex<dyn FnMut(&T) -> bool + Send + 'static>>;
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct ArcStatefulPredicate<T> {
pub(super) function: ArcStatefulPredicateFn<T>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T> ArcStatefulPredicate<T> {
impl_predicate_common_methods!(ArcStatefulPredicate<T>, (FnMut(&T) -> bool + Send + 'static), |f| {
Arc::new(Mutex::new(f))
});
#[inline]
pub fn and<P>(&self, mut other: P) -> ArcStatefulPredicate<T>
where
P: StatefulPredicate<T> + Send + 'static,
T: 'static,
{
let self_fn = self.function.clone();
ArcStatefulPredicate::new(move |value: &T| {
let matched = {
let mut function = self_fn.lock();
function(value)
};
matched && other.test(value)
})
}
#[inline]
pub fn or<P>(&self, mut other: P) -> ArcStatefulPredicate<T>
where
P: StatefulPredicate<T> + Send + 'static,
T: 'static,
{
let self_fn = self.function.clone();
ArcStatefulPredicate::new(move |value: &T| {
let matched = {
let mut function = self_fn.lock();
function(value)
};
matched || other.test(value)
})
}
#[inline]
pub fn nand<P>(&self, mut other: P) -> ArcStatefulPredicate<T>
where
P: StatefulPredicate<T> + Send + 'static,
T: 'static,
{
let self_fn = self.function.clone();
ArcStatefulPredicate::new(move |value: &T| {
let matched = {
let mut function = self_fn.lock();
function(value)
};
!(matched && other.test(value))
})
}
#[inline]
pub fn xor<P>(&self, mut other: P) -> ArcStatefulPredicate<T>
where
P: StatefulPredicate<T> + Send + 'static,
T: 'static,
{
let self_fn = self.function.clone();
ArcStatefulPredicate::new(move |value: &T| {
let matched = {
let mut function = self_fn.lock();
function(value)
};
matched ^ other.test(value)
})
}
#[inline]
pub fn nor<P>(&self, mut other: P) -> ArcStatefulPredicate<T>
where
P: StatefulPredicate<T> + Send + 'static,
T: 'static,
{
let self_fn = self.function.clone();
ArcStatefulPredicate::new(move |value: &T| {
let matched = {
let mut function = self_fn.lock();
function(value)
};
!(matched || other.test(value))
})
}
}
impl<T> Not for ArcStatefulPredicate<T>
where
T: 'static,
{
type Output = ArcStatefulPredicate<T>;
fn not(self) -> Self::Output {
let metadata = self.metadata;
let function = self.function;
ArcStatefulPredicate::new_with_metadata(
move |value: &T| {
let mut function = function.lock();
!function(value)
},
metadata,
)
}
}
impl<T> Not for &ArcStatefulPredicate<T>
where
T: 'static,
{
type Output = ArcStatefulPredicate<T>;
fn not(self) -> Self::Output {
let function = self.function.clone();
ArcStatefulPredicate::new_with_metadata(
move |value: &T| {
let mut function = function.lock();
!function(value)
},
self.metadata.clone(),
)
}
}
impl_predicate_clone!(ArcStatefulPredicate<T>);
impl_predicate_debug_display!(ArcStatefulPredicate<T>);
impl<T> StatefulPredicate<T> for ArcStatefulPredicate<T> {
#[inline]
fn test(&mut self, value: &T) -> bool {
let mut function = self.function.lock();
function(value)
}
}