Trait predicates::predicate::Predicate [] [src]

pub trait Predicate {
    type Item;
    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) -> Box<Predicate<Item = Self::Item>>
    where
        Self: 'static + Sized
, { ... } }

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

The type that this Predicate will accept for evaluating.

Required Methods

Execute this Predicate against variable, returning the resulting boolean.

Provided Methods

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));

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));

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));

Convenience function that returns a trait object of this Predicate.

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