use std::ops::Not;
use super::{
ALWAYS_FALSE_NAME,
ALWAYS_TRUE_NAME,
BiPredicate,
BiPredicateFn,
BoxBiPredicate,
Rc,
impl_predicate_clone,
impl_predicate_common_methods,
impl_predicate_debug_display,
impl_rc_conversions,
impl_shared_predicate_methods,
};
pub struct RcBiPredicate<T, U> {
pub(super) function: Rc<BiPredicateFn<T, U>>,
pub(super) name: Option<String>,
}
impl<T, U> RcBiPredicate<T, U> {
impl_predicate_common_methods!(
RcBiPredicate<T, U>,
(Fn(&T, &U) -> bool + 'static),
|f| Rc::new(f)
);
impl_shared_predicate_methods!(RcBiPredicate<T, U>, 'static);
}
impl<T, U> Not for RcBiPredicate<T, U>
where
T: 'static,
U: 'static,
{
type Output = RcBiPredicate<T, U>;
fn not(self) -> Self::Output {
let function = self.function;
RcBiPredicate::new(move |first, second| !function(first, second))
}
}
impl<T, U> Not for &RcBiPredicate<T, U>
where
T: 'static,
U: 'static,
{
type Output = RcBiPredicate<T, U>;
fn not(self) -> Self::Output {
let function = self.function.clone();
RcBiPredicate::new(move |first, second| !function(first, second))
}
}
impl_predicate_clone!(RcBiPredicate<T, U>);
impl_predicate_debug_display!(RcBiPredicate<T, U>);
impl<T, U> BiPredicate<T, U> for RcBiPredicate<T, U> {
fn test(&self, first: &T, second: &U) -> bool {
(self.function)(first, second)
}
impl_rc_conversions!(
RcBiPredicate<T, U>,
BoxBiPredicate,
Fn(first: &T, second: &U) -> bool
);
}