diff --git a/src/iter.rs b/src/iter.rs index c8c97fc..16c2dcf 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -60,6 +60,11 @@ where /// assert_eq!(true, predicate_fn.eval("a")); /// assert_eq!(false, predicate_fn.eval("b")); /// assert_eq!(true, predicate_fn.eval("c")); + /// + /// let predicate_fn = predicate::in_iter(vec![String::from("a"), String::from("c"), String::from("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 { let mut items = self.inner.debug; @@ -70,33 +75,16 @@ where } } -impl Predicate for InPredicate -where - T: PartialEq + fmt::Debug, -{ - fn eval(&self, variable: &T) -> bool { - self.inner.debug.contains(variable) - } - - fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option> { - utils::default_find_case(self, expected, variable).map(|case| { - case.add_product(reflection::Product::new( - "var", - utils::DebugAdapter::new(variable).to_string(), - )) - }) - } -} - -impl<'a, T> Predicate for InPredicate<&'a T> +impl Predicate

for InPredicate where - T: PartialEq + fmt::Debug + ?Sized, + T: std::borrow::Borrow

+ PartialEq + fmt::Debug, + P: PartialEq + fmt::Debug + ?Sized, { - fn eval(&self, variable: &T) -> bool { - self.inner.debug.contains(&variable) + fn eval(&self, variable: &P) -> bool { + self.inner.debug.iter().any(|x| x.borrow() == variable) } - fn find_case<'b>(&'b self, expected: bool, variable: &T) -> Option> { + fn find_case<'a>(&'a self, expected: bool, variable: &P) -> Option> { utils::default_find_case(self, expected, variable).map(|case| { case.add_product(reflection::Product::new( "var", @@ -160,6 +148,11 @@ where /// assert_eq!(true, predicate_fn.eval("a")); /// assert_eq!(false, predicate_fn.eval("b")); /// assert_eq!(true, predicate_fn.eval("c")); +/// +/// let predicate_fn = predicate::in_iter(vec![String::from("a"), String::from("c"), String::from("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(iter: I) -> InPredicate where @@ -188,33 +181,19 @@ where inner: utils::DebugAdapter>, } -impl Predicate for OrdInPredicate +impl Predicate

for OrdInPredicate where - T: Ord + fmt::Debug, + T: std::borrow::Borrow

+ Ord + fmt::Debug, + P: Ord + fmt::Debug + ?Sized, { - fn eval(&self, variable: &T) -> bool { - self.inner.debug.binary_search(variable).is_ok() + fn eval(&self, variable: &P) -> bool { + self.inner + .debug + .binary_search_by(|x| x.borrow().cmp(variable)) + .is_ok() } - fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option> { - utils::default_find_case(self, expected, variable).map(|case| { - case.add_product(reflection::Product::new( - "var", - utils::DebugAdapter::new(variable).to_string(), - )) - }) - } -} - -impl<'a, T> Predicate for OrdInPredicate<&'a T> -where - T: Ord + fmt::Debug + ?Sized, -{ - fn eval(&self, variable: &T) -> bool { - self.inner.debug.binary_search(&variable).is_ok() - } - - fn find_case<'b>(&'b self, expected: bool, variable: &T) -> Option> { + fn find_case<'a>(&'a self, expected: bool, variable: &P) -> Option> { utils::default_find_case(self, expected, variable).map(|case| { case.add_product(reflection::Product::new( "var", @@ -267,33 +246,16 @@ where inner: utils::DebugAdapter>, } -impl Predicate for HashableInPredicate +impl Predicate

for HashableInPredicate where - T: Hash + Eq + fmt::Debug, + T: std::borrow::Borrow

+ Hash + Eq + fmt::Debug, + P: Hash + Eq + fmt::Debug + ?Sized, { - fn eval(&self, variable: &T) -> bool { + fn eval(&self, variable: &P) -> bool { self.inner.debug.contains(variable) } - fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option> { - utils::default_find_case(self, expected, variable).map(|case| { - case.add_product(reflection::Product::new( - "var", - utils::DebugAdapter::new(variable).to_string(), - )) - }) - } -} - -impl<'a, T> Predicate for HashableInPredicate<&'a T> -where - T: Hash + Eq + fmt::Debug + ?Sized, -{ - fn eval(&self, variable: &T) -> bool { - self.inner.debug.contains(&variable) - } - - fn find_case<'b>(&'b self, expected: bool, variable: &T) -> Option> { + fn find_case<'a>(&'a self, expected: bool, variable: &P) -> Option> { utils::default_find_case(self, expected, variable).map(|case| { case.add_product(reflection::Product::new( "var", @@ -351,6 +313,11 @@ where /// assert_eq!(true, predicate_fn.eval("a")); /// assert_eq!(false, predicate_fn.eval("b")); /// assert_eq!(true, predicate_fn.eval("c")); +/// +/// let predicate_fn = predicate::in_hash(vec![String::from("a"), String::from("c"), String::from("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(iter: I) -> HashableInPredicate where diff --git a/src/ord.rs b/src/ord.rs index aa768cb..0629736 100644 --- a/src/ord.rs +++ b/src/ord.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2018 The predicates-rs Project Developers. +// Copyright (c) 2018, 2022 The predicates-rs Project Developers. // // Licensed under the Apache License, Version 2.0 or the MIT license @@ -35,26 +35,24 @@ impl fmt::Display for EqOps { /// /// This is created by the `predicate::{eq, ne}` functions. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct EqPredicate -where - T: fmt::Debug + PartialEq, -{ +pub struct EqPredicate { constant: T, op: EqOps, } -impl Predicate for EqPredicate +impl Predicate

for EqPredicate where - T: fmt::Debug + PartialEq, + T: std::borrow::Borrow

+ fmt::Debug, + P: fmt::Debug + PartialEq + ?Sized, { - fn eval(&self, variable: &T) -> bool { + fn eval(&self, variable: &P) -> bool { match self.op { - EqOps::Equal => variable.eq(&self.constant), - EqOps::NotEqual => variable.ne(&self.constant), + EqOps::Equal => variable.eq(self.constant.borrow()), + EqOps::NotEqual => variable.ne(self.constant.borrow()), } } - fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option> { + fn find_case<'a>(&'a self, expected: bool, variable: &P) -> Option> { utils::default_find_case(self, expected, variable).map(|case| { case.add_product(reflection::Product::new( "var", @@ -64,32 +62,11 @@ where } } -impl<'a, T> Predicate for EqPredicate<&'a T> -where - T: fmt::Debug + PartialEq + ?Sized, -{ - fn eval(&self, variable: &T) -> bool { - match self.op { - EqOps::Equal => variable.eq(self.constant), - EqOps::NotEqual => variable.ne(self.constant), - } - } - - fn find_case<'b>(&'b self, expected: bool, variable: &T) -> Option> { - utils::default_find_case(self, expected, variable).map(|case| { - case.add_product(reflection::Product::new( - "var", - utils::DebugAdapter::new(variable).to_string(), - )) - }) - } -} - -impl reflection::PredicateReflection for EqPredicate where T: fmt::Debug + PartialEq {} +impl reflection::PredicateReflection for EqPredicate where T: fmt::Debug {} impl fmt::Display for EqPredicate where - T: fmt::Debug + PartialEq, + T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let palette = crate::Palette::current(); @@ -120,6 +97,10 @@ where /// let predicate_fn = predicate::eq("Hello"); /// assert_eq!(true, predicate_fn.eval("Hello")); /// assert_eq!(false, predicate_fn.eval("Goodbye")); +/// +/// let predicate_fn = predicate::eq(String::from("Hello")); +/// assert_eq!(true, predicate_fn.eval("Hello")); +/// assert_eq!(false, predicate_fn.eval("Goodbye")); /// ``` pub fn eq(constant: T) -> EqPredicate where @@ -178,28 +159,26 @@ impl fmt::Display for OrdOps { /// /// This is created by the `predicate::{gt, ge, lt, le}` functions. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct OrdPredicate -where - T: fmt::Debug + PartialOrd, -{ +pub struct OrdPredicate { constant: T, op: OrdOps, } -impl Predicate for OrdPredicate +impl Predicate

for OrdPredicate where - T: fmt::Debug + PartialOrd, + T: std::borrow::Borrow

+ fmt::Debug, + P: fmt::Debug + PartialOrd + ?Sized, { - fn eval(&self, variable: &T) -> bool { + fn eval(&self, variable: &P) -> 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), + OrdOps::LessThan => variable.lt(self.constant.borrow()), + OrdOps::LessThanOrEqual => variable.le(self.constant.borrow()), + OrdOps::GreaterThanOrEqual => variable.ge(self.constant.borrow()), + OrdOps::GreaterThan => variable.gt(self.constant.borrow()), } } - fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option> { + fn find_case<'a>(&'a self, expected: bool, variable: &P) -> Option> { utils::default_find_case(self, expected, variable).map(|case| { case.add_product(reflection::Product::new( "var", @@ -209,34 +188,11 @@ where } } -impl<'a, T> Predicate for OrdPredicate<&'a T> -where - T: fmt::Debug + PartialOrd + ?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), - } - } - - fn find_case<'b>(&'b self, expected: bool, variable: &T) -> Option> { - utils::default_find_case(self, expected, variable).map(|case| { - case.add_product(reflection::Product::new( - "var", - utils::DebugAdapter::new(variable).to_string(), - )) - }) - } -} - -impl reflection::PredicateReflection for OrdPredicate where T: fmt::Debug + PartialOrd {} +impl reflection::PredicateReflection for OrdPredicate where T: fmt::Debug {} impl fmt::Display for OrdPredicate where - T: fmt::Debug + PartialOrd, + T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let palette = crate::Palette::current(); @@ -267,6 +223,10 @@ where /// let predicate_fn = predicate::lt("b"); /// assert_eq!(true, predicate_fn.eval("a")); /// assert_eq!(false, predicate_fn.eval("c")); +/// +/// let predicate_fn = predicate::lt(String::from("b")); +/// assert_eq!(true, predicate_fn.eval("a")); +/// assert_eq!(false, predicate_fn.eval("c")); /// ``` pub fn lt(constant: T) -> OrdPredicate where