Trait predicates::Predicate [] [src]

pub trait Predicate<Item: ?Sized> {
    fn eval(&self, variable: &Item) -> bool;

    fn and<B>(self, other: B) -> AndPredicate<Self, B, Item>
    where
        B: Predicate<Item>,
        Self: Sized
, { ... }
fn or<B>(self, other: B) -> OrPredicate<Self, B, Item>
    where
        B: Predicate<Item>,
        Self: Sized
, { ... }
fn not(self) -> NotPredicate<Self, Item>
    where
        Self: Sized
, { ... }
fn boxed(self) -> BoxPredicate<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.

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::prelude::*;

let predicate_fn1 = predicate::always().and(predicate::always());
let predicate_fn2 = predicate::always().and(predicate::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::prelude::*;

let predicate_fn1 = predicate::always().or(predicate::always());
let predicate_fn2 = predicate::always().or(predicate::never());
let predicate_fn3 = predicate::never().or(predicate::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::prelude::*;

let predicate_fn1 = predicate::always().not();
let predicate_fn2 = predicate::never().not();
assert_eq!(false, predicate_fn1.eval(&4));
assert_eq!(true, predicate_fn2.eval(&4));

Returns a BoxPredicate wrapper around this Predicate type.

Returns a BoxPredicate wrapper around this Predicate type. TheBoxPredicate` 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 and Display.

Examples

use predicates::prelude::*;

let predicates = vec![
    predicate::always().boxed(),
    predicate::never().boxed(),
];
assert_eq!(true, predicates[0].eval(&4));
assert_eq!(false, predicates[1].eval(&4));

Implementors