Skip to content

Commit f981fac

Browse files
committed
fix(predicate): Break out boxing from trait
BREAKING CHANGE: `Predicate` trait is changed but most use cases should not be impacted.
1 parent 88b72f9 commit f981fac

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

src/boxed.rs

+43
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,46 @@ where
5757
self.0.eval(variable)
5858
}
5959
}
60+
61+
/// `Predicate` extension for boxing a `Predicate`.
62+
pub trait PredicateBoxExt<Item: ?Sized>
63+
where
64+
Self: Predicate<Item>,
65+
{
66+
/// Returns a `BoxPredicate` wrapper around this `Predicate` type.
67+
///
68+
/// Returns a `BoxPredicate` wrapper around this `Predicate type. The
69+
/// `BoxPredicate` type has a number of useful properties:
70+
///
71+
/// - It stores the inner predicate as a trait object, so the type of
72+
/// `BoxPredicate` will always be the same even if steps are added or
73+
/// removed from the predicate.
74+
/// - It is a common type, allowing it to be stored in vectors or other
75+
/// collection types.
76+
/// - It implements `Debug` and `Display`.
77+
///
78+
/// # Examples
79+
///
80+
/// ```
81+
/// use predicates::prelude::*;
82+
///
83+
/// let predicates = vec![
84+
/// predicate::always().boxed(),
85+
/// predicate::never().boxed(),
86+
/// ];
87+
/// assert_eq!(true, predicates[0].eval(&4));
88+
/// assert_eq!(false, predicates[1].eval(&4));
89+
/// ```
90+
fn boxed(self) -> BoxPredicate<Item>
91+
where
92+
Self: Sized + Send + Sync + 'static,
93+
{
94+
BoxPredicate::new(self)
95+
}
96+
}
97+
98+
impl<P, Item> PredicateBoxExt<Item> for P
99+
where
100+
P: Predicate<Item>,
101+
{
102+
}

src/core.rs

-33
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use boxed::BoxPredicate;
10-
119
/// Trait for generically evaluating a type against a dynamically created
1210
/// predicate function.
1311
///
@@ -19,35 +17,4 @@ pub trait Predicate<Item: ?Sized> {
1917
/// Execute this `Predicate` against `variable`, returning the resulting
2018
/// boolean.
2119
fn eval(&self, variable: &Item) -> bool;
22-
23-
/// Returns a `BoxPredicate` wrapper around this `Predicate` type.
24-
///
25-
/// Returns a `BoxPredicate` wrapper around this `Predicate type. The
26-
/// `BoxPredicate` type has a number of useful properties:
27-
///
28-
/// - It stores the inner predicate as a trait object, so the type of
29-
/// `BoxPredicate` will always be the same even if steps are added or
30-
/// removed from the predicate.
31-
/// - It is a common type, allowing it to be stored in vectors or other
32-
/// collection types.
33-
/// - It implements `Debug` and `Display`.
34-
///
35-
/// # Examples
36-
///
37-
/// ```
38-
/// use predicates::prelude::*;
39-
///
40-
/// let predicates = vec![
41-
/// predicate::always().boxed(),
42-
/// predicate::never().boxed(),
43-
/// ];
44-
/// assert_eq!(true, predicates[0].eval(&4));
45-
/// assert_eq!(false, predicates[1].eval(&4));
46-
/// ```
47-
fn boxed(self) -> BoxPredicate<Item>
48-
where
49-
Self: Sized + Send + Sync + 'static,
50-
{
51-
BoxPredicate::new(self)
52-
}
5320
}

src/prelude.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
pub use core::Predicate;
1212
pub use boolean::PredicateBooleanExt;
13+
pub use boxed::PredicateBoxExt;
1314

1415
/// Predicate factories
1516
pub mod predicate {

0 commit comments

Comments
 (0)