pub trait Predicate<T> {
// Required methods
fn test(value: &T) -> bool;
fn error() -> ErrorMessage;
// Provided method
unsafe fn optimize(_value: &T) { ... }
}Expand description
An assertion that must hold for an instance of a type to be considered refined.
Required Methods§
Sourcefn test(value: &T) -> bool
fn test(value: &T) -> bool
Whether a value satisfies the predicate.
§Correctness
Implementations of this method must be pure functions. They must be infallible and
must always return the same result when provided the same input value. If you have a
situation that requires impurity to “materialize” a predicate, use the Default::default
implementation of a StatefulPredicate. Even then, under no circumstance can the test
function itself be impure.
Sourcefn error() -> ErrorMessage
fn error() -> ErrorMessage
An error message to display when the predicate doesn’t hold.
Provided Methods§
Sourceunsafe fn optimize(_value: &T)
unsafe fn optimize(_value: &T)
Applies a potentially unsafe optimization to call sites that can take advantage of
information provided by the predicate. This function is unused by refined unless
the optimized feature is enabled.
As all predicate tests should be pure, implementing this function is recommended for most predicate implementations. The most common implementation will look like:
unsafe fn optimize(value: &T) {
core::hint::assert_unchecked(Self::test(value));
}Note that core::hint::assert_unchecked acts as an assert in debug builds, meaning that tests should be able to detect correctness issues as well. It is only in optimized builds that no check is performed.
§Safety
Implementation of this function takes a correctness property and turns it in to a soundness property. This means that incorrect implementation of this function can lead to undefined behavior. If you have any doubt about the purity of your test implementation, do not implement this function (and, probably, you should reconsider your approach).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl Predicate<char> for IsControl
impl Predicate<char> for IsDigit
impl Predicate<char> for IsHexDigit
impl Predicate<char> for IsLowercase
impl Predicate<char> for IsNumeric
impl Predicate<char> for IsUppercase
impl Predicate<char> for IsWhitespace
impl<S: TypeString, T: AsRef<str>> Predicate<T> for Regex<S>
regex and alloc only.impl<T> Predicate<T> for False
impl<T> Predicate<T> for True
impl<T, A: Predicate<T>, B: Predicate<T>> Predicate<T> for And<A, B>
impl<T, A: Predicate<T>, B: Predicate<T>> Predicate<T> for Or<A, B>
impl<T, A: Predicate<T>, B: Predicate<T>> Predicate<T> for Xor<A, B>
impl<T, P: Predicate<T>> Predicate<T> for Not<P>
impl<T: SignedBoundable, const DIV: isize, const MOD: isize> Predicate<T> for refined::boundable::signed::Modulo<DIV, MOD>
impl<T: SignedBoundable, const MAX: isize> Predicate<T> for refined::boundable::signed::LessThan<MAX>
impl<T: SignedBoundable, const MAX: isize> Predicate<T> for refined::boundable::signed::LessThanEqual<MAX>
impl<T: SignedBoundable, const MIN: isize> Predicate<T> for refined::boundable::signed::GreaterThan<MIN>
impl<T: SignedBoundable, const MIN: isize> Predicate<T> for refined::boundable::signed::GreaterThanEqual<MIN>
impl<T: SignedBoundable, const VAL: isize> Predicate<T> for refined::boundable::signed::Equals<VAL>
impl<T: UnsignedBoundable, const DIV: usize, const MOD: usize> Predicate<T> for refined::boundable::unsigned::Modulo<DIV, MOD>
impl<T: UnsignedBoundable, const MAX: usize> Predicate<T> for refined::boundable::unsigned::LessThan<MAX>
impl<T: UnsignedBoundable, const MAX: usize> Predicate<T> for refined::boundable::unsigned::LessThanEqual<MAX>
impl<T: UnsignedBoundable, const MIN: usize> Predicate<T> for refined::boundable::unsigned::GreaterThan<MIN>
impl<T: UnsignedBoundable, const MIN: usize> Predicate<T> for refined::boundable::unsigned::GreaterThanEqual<MIN>
impl<T: UnsignedBoundable, const VAL: usize> Predicate<T> for refined::boundable::unsigned::Equals<VAL>
impl<T: AsRef<str>> Predicate<T> for Trimmed
alloc only.impl<T: AsRef<str>, Prefix: TypeString> Predicate<T> for StartsWith<Prefix>
alloc only.impl<T: AsRef<str>, Substr: TypeString> Predicate<T> for Contains<Substr>
alloc only.impl<T: AsRef<str>, Suffix: TypeString> Predicate<T> for EndsWith<Suffix>
alloc only.