// Copyright (c) 2018, 2022 The predicates-rs Project Developers. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/license/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! Definition of `Predicate`s for comparisons over `Ord` and `Eq` types.
impl fmt::Display for EqOps { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let op = match *self {
EqOps::Equal => "==",
EqOps::NotEqual => "!=",
};
write!(f, "{op}")
}
}
/// Predicate that returns `true` if `variable` matches the pre-defined `Eq` /// value, otherwise returns `false`. /// /// This is created by the `predicate::{eq, ne}` functions. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pubstruct EqPredicate<T> {
constant: T,
op: EqOps,
}
/// Creates a new predicate that will return `true` when the given `variable` is /// equal to a pre-defined value. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// 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")); /// /// let predicate_fn = predicate::eq(String::from("Hello")); /// assert_eq!(true, predicate_fn.eval("Hello")); /// assert_eq!(false, predicate_fn.eval("Goodbye")); /// ``` pubfn eq<T>(constant: T) -> EqPredicate<T> where
T: fmt::Debug + PartialEq,
{
EqPredicate {
constant,
op: EqOps::Equal,
}
}
/// Creates a new predicate that will return `true` when the given `variable` is /// _not_ equal to a pre-defined value. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let predicate_fn = predicate::ne(5); /// assert_eq!(false, predicate_fn.eval(&5)); /// assert_eq!(true, predicate_fn.eval(&10)); /// ``` pubfn ne<T>(constant: T) -> EqPredicate<T> where
T: PartialEq + fmt::Debug,
{
EqPredicate {
constant,
op: EqOps::NotEqual,
}
}
/// Creates a new predicate that will return `true` when the given `variable` is /// less than a pre-defined value. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// 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")); /// /// let predicate_fn = predicate::lt(String::from("b")); /// assert_eq!(true, predicate_fn.eval("a")); /// assert_eq!(false, predicate_fn.eval("c")); /// ``` pubfn lt<T>(constant: T) -> OrdPredicate<T> where
T: fmt::Debug + PartialOrd,
{
OrdPredicate {
constant,
op: OrdOps::LessThan,
}
}
/// Creates a new predicate that will return `true` when the given `variable` is /// less than or equal to a pre-defined value. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let predicate_fn = predicate::le(5); /// assert_eq!(true, predicate_fn.eval(&4)); /// assert_eq!(true, predicate_fn.eval(&5)); /// assert_eq!(false, predicate_fn.eval(&6)); /// ``` pubfn le<T>(constant: T) -> OrdPredicate<T> where
T: PartialOrd + fmt::Debug,
{
OrdPredicate {
constant,
op: OrdOps::LessThanOrEqual,
}
}
/// Creates a new predicate that will return `true` when the given `variable` is /// greater than or equal to a pre-defined value. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let predicate = predicate::ge(5); /// assert_eq!(false, predicate.eval(&4)); /// assert_eq!(true, predicate.eval(&5)); /// assert_eq!(true, predicate.eval(&6)); /// ``` pubfn ge<T>(constant: T) -> OrdPredicate<T> where
T: PartialOrd + fmt::Debug,
{
OrdPredicate {
constant,
op: OrdOps::GreaterThanOrEqual,
}
}
/// Creates a new predicate that will return `true` when the given `variable` is /// greater than a pre-defined value. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let predicate_fn = predicate::gt(5); /// assert_eq!(false, predicate_fn.eval(&4)); /// assert_eq!(false, predicate_fn.eval(&5)); /// assert_eq!(true, predicate_fn.eval(&6)); /// ``` pubfn gt<T>(constant: T) -> OrdPredicate<T> where
T: PartialOrd + fmt::Debug,
{
OrdPredicate {
constant,
op: OrdOps::GreaterThan,
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-10-03)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.