Trait predicates::predicate::Predicate
[−]
[src]
pub trait Predicate { type Item: ?Sized; fn eval(&self, variable: &Self::Item) -> bool; fn and<B>(self, other: B) -> AndPredicate<Self, B>
where
B: Predicate<Item = Self::Item>,
Self: Sized, { ... } fn or<B>(self, other: B) -> OrPredicate<Self, B>
where
B: Predicate<Item = Self::Item>,
Self: Sized, { ... } fn not(self) -> NotPredicate<Self>
where
Self: Sized, { ... } fn boxed(self) -> BoxPredicate<Self::Item>
where
Self: Sized + Send + Sync + 'static, { ... } }
Trait for generically evaluating a type against a dynamically created predicate function.
The exact meaning of eval
depends on the situation, but will usually
mean that the evaluated item is in some sort of pre-defined set. This is
different from Ord
and Eq
in that an item
will almost never be the
same type as the implementing Predicate
type.
Associated Types
Required Methods
fn eval(&self, variable: &Self::Item) -> bool
Execute this Predicate
against variable
, returning the resulting
boolean.
Provided Methods
fn and<B>(self, other: B) -> AndPredicate<Self, B> where
B: Predicate<Item = Self::Item>,
Self: Sized,
B: Predicate<Item = Self::Item>,
Self: Sized,
Compute the logical AND of two Predicate
results, returning the result.
Examples
use predicates::predicate::*; let predicate_fn1 = always().and(always()); let predicate_fn2 = always().and(never()); assert_eq!(true, predicate_fn1.eval(&4)); assert_eq!(false, predicate_fn2.eval(&4));
fn or<B>(self, other: B) -> OrPredicate<Self, B> where
B: Predicate<Item = Self::Item>,
Self: Sized,
B: Predicate<Item = Self::Item>,
Self: Sized,
Compute the logical OR of two Predicate
results, returning the result.
Examples
use predicates::predicate::*; let predicate_fn1 = always().or(always()); let predicate_fn2 = always().or(never()); let predicate_fn3 = never().or(never()); assert_eq!(true, predicate_fn1.eval(&4)); assert_eq!(true, predicate_fn2.eval(&4)); assert_eq!(false, predicate_fn3.eval(&4));
fn not(self) -> NotPredicate<Self> where
Self: Sized,
Self: Sized,
Compute the logical NOT of a Predicate
, returning the result.
Examples
use predicates::predicate::*; let predicate_fn1 = always().not(); let predicate_fn2 = never().not(); assert_eq!(false, predicate_fn1.eval(&4)); assert_eq!(true, predicate_fn2.eval(&4));
fn boxed(self) -> BoxPredicate<Self::Item> where
Self: Sized + Send + Sync + 'static,
Self: Sized + Send + Sync + 'static,
Returns a BoxPredicate
wrapper around this Predicate
type.
Returns a BoxPredicate
wrapper around this Predicate type. The
BoxPredicate` type has a number of useful properties:
- It stores the inner predicate as a trait object, so the type of
BoxPredicate
will always be the same even if steps are added or removed from the predicate. - It is a common type, allowing it to be stored in vectors or other collection types.
- It implements
Debug
andDisplay
.
Examples
use predicates::predicate::*; let predicates = vec![ always().boxed(), never().boxed(), ]; assert_eq!(true, predicates[0].eval(&4)); assert_eq!(false, predicates[1].eval(&4));
Implementors
impl<T> Predicate for BooleanPredicate<T>
impl<F, T> Predicate for FnPredicate<F, T> where
F: Fn(&T) -> bool,impl<T> Predicate for EqPredicate<T> where
T: PartialEq,impl<T> Predicate for OrdPredicate<T> where
T: PartialOrd,impl<T> Predicate for ContainsPredicate<T> where
T: PartialEq,impl<T> Predicate for OrdContainsPredicate<T> where
T: Ord,impl<T> Predicate for HashableContainsPredicate<T> where
T: Hash + Eq,impl<M1, M2> Predicate for AndPredicate<M1, M2> where
M1: Predicate,
M2: Predicate<Item = M1::Item>,impl<M1, M2> Predicate for OrPredicate<M1, M2> where
M1: Predicate,
M2: Predicate<Item = M1::Item>,impl<M> Predicate for NotPredicate<M> where
M: Predicate,impl<T: ?Sized> Predicate for BoxPredicate<T>