Skip to content

Commit

Permalink
fix(predicate): Break out boxing from trait
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `Predicate` trait is changed but most use cases should
not be impacted.
  • Loading branch information
epage committed May 11, 2018
1 parent 88b72f9 commit f981fac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
43 changes: 43 additions & 0 deletions src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,46 @@ where
self.0.eval(variable)
}
}

/// `Predicate` extension for boxing a `Predicate`.
pub trait PredicateBoxExt<Item: ?Sized>
where
Self: Predicate<Item>,
{
/// 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` 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));
/// ```
fn boxed(self) -> BoxPredicate<Item>
where
Self: Sized + Send + Sync + 'static,
{
BoxPredicate::new(self)
}
}

impl<P, Item> PredicateBoxExt<Item> for P
where
P: Predicate<Item>,
{
}
33 changes: 0 additions & 33 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use boxed::BoxPredicate;

/// Trait for generically evaluating a type against a dynamically created
/// predicate function.
///
Expand All @@ -19,35 +17,4 @@ pub trait Predicate<Item: ?Sized> {
/// Execute this `Predicate` against `variable`, returning the resulting
/// boolean.
fn eval(&self, variable: &Item) -> bool;

/// 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` 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));
/// ```
fn boxed(self) -> BoxPredicate<Item>
where
Self: Sized + Send + Sync + 'static,
{
BoxPredicate::new(self)
}
}
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

pub use core::Predicate;
pub use boolean::PredicateBooleanExt;
pub use boxed::PredicateBoxExt;

/// Predicate factories
pub mod predicate {
Expand Down

0 comments on commit f981fac

Please sign in to comment.