Skip to content

Commit

Permalink
feat: Fix eq for str
Browse files Browse the repository at this point in the history
`eq("str")` wouldn't produce a `Predicate<str>` but instead a
`Predicate<&str>`, requiring the user to pass in a `&&str`.
  • Loading branch information
epage committed Jun 6, 2018
1 parent 8864e29 commit 7650e9e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ where
/// assert_eq!(true, predicate_fn.eval(&1));
/// assert_eq!(false, predicate_fn.eval(&2));
/// assert_eq!(true, predicate_fn.eval(&3));
/// assert_eq!(false, predicate_fn.eval(&4));
/// assert_eq!(true, predicate_fn.eval(&5));
///
/// let predicate_fn = predicate::in_iter(vec!["a", "c", "e"]).sort();
/// assert_eq!(true, predicate_fn.eval("a"));
/// assert_eq!(false, predicate_fn.eval("b"));
/// assert_eq!(true, predicate_fn.eval("c"));
/// ```
pub fn sort(self) -> OrdInPredicate<T> {
let mut items = self.inner;
Expand All @@ -72,6 +75,15 @@ where
}
}

impl<'a, T> Predicate<T> for InPredicate<&'a T>
where
T: PartialEq + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
self.inner.contains(&variable)
}
}

impl<T> fmt::Display for InPredicate<T>
where
T: PartialEq + fmt::Debug,
Expand Down Expand Up @@ -104,8 +116,11 @@ where
/// assert_eq!(true, predicate_fn.eval(&1));
/// assert_eq!(false, predicate_fn.eval(&2));
/// assert_eq!(true, predicate_fn.eval(&3));
/// assert_eq!(false, predicate_fn.eval(&4));
/// assert_eq!(true, predicate_fn.eval(&5));
///
/// let predicate_fn = predicate::in_iter(vec!["a", "c", "e"]);
/// assert_eq!(true, predicate_fn.eval("a"));
/// assert_eq!(false, predicate_fn.eval("b"));
/// assert_eq!(true, predicate_fn.eval("c"));
/// ```
pub fn in_iter<I, T>(iter: I) -> InPredicate<T>
where
Expand Down Expand Up @@ -143,6 +158,15 @@ where
}
}

impl<'a, T> Predicate<T> for OrdInPredicate<&'a T>
where
T: Ord + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
self.inner.binary_search(&variable).is_ok()
}
}

impl<T> fmt::Display for OrdInPredicate<T>
where
T: Ord + fmt::Debug,
Expand Down Expand Up @@ -178,6 +202,15 @@ where
}
}

impl<'a, T> Predicate<T> for HashableInPredicate<&'a T>
where
T: Hash + Eq + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
self.inner.contains(&variable)
}
}

impl<T> fmt::Display for HashableInPredicate<T>
where
T: Hash + Eq + fmt::Debug,
Expand All @@ -204,8 +237,11 @@ where
/// assert_eq!(true, predicate_fn.eval(&1));
/// assert_eq!(false, predicate_fn.eval(&2));
/// assert_eq!(true, predicate_fn.eval(&3));
/// assert_eq!(false, predicate_fn.eval(&4));
/// assert_eq!(true, predicate_fn.eval(&5));
///
/// let predicate_fn = predicate::in_hash(vec!["a", "c", "e"]);
/// assert_eq!(true, predicate_fn.eval("a"));
/// assert_eq!(false, predicate_fn.eval("b"));
/// assert_eq!(true, predicate_fn.eval("c"));
/// ```
pub fn in_hash<I, T>(iter: I) -> HashableInPredicate<T>
where
Expand Down
34 changes: 34 additions & 0 deletions src/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ where
}
}

impl<'a, T> Predicate<T> for EqPredicate<&'a T>
where
T: PartialEq + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
match self.op {
EqOps::Equal => variable.eq(self.constant),
EqOps::NotEqual => variable.ne(self.constant),
}
}
}

impl<T> fmt::Display for EqPredicate<T>
where
T: fmt::Debug,
Expand All @@ -73,6 +85,10 @@ where
/// let predicate_fn = predicate::eq(5);
/// assert_eq!(true, predicate_fn.eval(&5));
/// assert_eq!(false, predicate_fn.eval(&10));
///
/// let predicate_fn = predicate::eq("Hello");
/// assert_eq!(true, predicate_fn.eval("Hello"));
/// assert_eq!(false, predicate_fn.eval("Goodbye"));
/// ```
pub fn eq<T>(constant: T) -> EqPredicate<T>
where
Expand Down Expand Up @@ -153,6 +169,20 @@ where
}
}

impl<'a, T> Predicate<T> for OrdPredicate<&'a T>
where
T: PartialOrd + fmt::Debug + ?Sized,
{
fn eval(&self, variable: &T) -> bool {
match self.op {
OrdOps::LessThan => variable.lt(self.constant),
OrdOps::LessThanOrEqual => variable.le(self.constant),
OrdOps::GreaterThanOrEqual => variable.ge(self.constant),
OrdOps::GreaterThan => variable.gt(self.constant),
}
}
}

impl<T> fmt::Display for OrdPredicate<T>
where
T: fmt::Debug,
Expand All @@ -173,6 +203,10 @@ where
/// let predicate_fn = predicate::lt(5);
/// assert_eq!(true, predicate_fn.eval(&4));
/// assert_eq!(false, predicate_fn.eval(&6));
///
/// let predicate_fn = predicate::lt("b");
/// assert_eq!(true, predicate_fn.eval("a"));
/// assert_eq!(false, predicate_fn.eval("c"));
/// ```
pub fn lt<T>(constant: T) -> OrdPredicate<T>
where
Expand Down

0 comments on commit 7650e9e

Please sign in to comment.