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