Predicate

Trait Predicate 

Source
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§

Source

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.

Source

fn error() -> ErrorMessage

An error message to display when the predicate doesn’t hold.

Provided Methods§

Source

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§

Source§

impl Predicate<char> for IsControl

Source§

impl Predicate<char> for IsDigit

Source§

impl Predicate<char> for IsHexDigit

Source§

impl Predicate<char> for IsLowercase

Source§

impl Predicate<char> for IsNumeric

Source§

impl Predicate<char> for IsUppercase

Source§

impl Predicate<char> for IsWhitespace

Source§

impl<S: TypeString, T: AsRef<str>> Predicate<T> for Regex<S>

Available on crate features regex and alloc only.
Source§

impl<T> Predicate<T> for False

Source§

impl<T> Predicate<T> for True

Source§

impl<T, A: Predicate<T>, B: Predicate<T>> Predicate<T> for And<A, B>

Source§

impl<T, A: Predicate<T>, B: Predicate<T>> Predicate<T> for Or<A, B>

Source§

impl<T, A: Predicate<T>, B: Predicate<T>> Predicate<T> for Xor<A, B>

Source§

impl<T, P: Predicate<T>> Predicate<T> for Not<P>

Source§

impl<T: SignedBoundable, const DIV: isize, const MOD: isize> Predicate<T> for refined::boundable::signed::Modulo<DIV, MOD>

Source§

impl<T: SignedBoundable, const MAX: isize> Predicate<T> for refined::boundable::signed::LessThan<MAX>

Source§

impl<T: SignedBoundable, const MAX: isize> Predicate<T> for refined::boundable::signed::LessThanEqual<MAX>

Source§

impl<T: SignedBoundable, const MIN: isize> Predicate<T> for refined::boundable::signed::GreaterThan<MIN>

Source§

impl<T: SignedBoundable, const MIN: isize> Predicate<T> for refined::boundable::signed::GreaterThanEqual<MIN>

Source§

impl<T: SignedBoundable, const VAL: isize> Predicate<T> for refined::boundable::signed::Equals<VAL>

Source§

impl<T: UnsignedBoundable, const DIV: usize, const MOD: usize> Predicate<T> for refined::boundable::unsigned::Modulo<DIV, MOD>

Source§

impl<T: UnsignedBoundable, const MAX: usize> Predicate<T> for refined::boundable::unsigned::LessThan<MAX>

Source§

impl<T: UnsignedBoundable, const MAX: usize> Predicate<T> for refined::boundable::unsigned::LessThanEqual<MAX>

Source§

impl<T: UnsignedBoundable, const MIN: usize> Predicate<T> for refined::boundable::unsigned::GreaterThan<MIN>

Source§

impl<T: UnsignedBoundable, const MIN: usize> Predicate<T> for refined::boundable::unsigned::GreaterThanEqual<MIN>

Source§

impl<T: UnsignedBoundable, const VAL: usize> Predicate<T> for refined::boundable::unsigned::Equals<VAL>

Source§

impl<T: AsRef<str>> Predicate<T> for Trimmed

Available on crate feature alloc only.
Source§

impl<T: AsRef<str>, Prefix: TypeString> Predicate<T> for StartsWith<Prefix>

Available on crate feature alloc only.
Source§

impl<T: AsRef<str>, Substr: TypeString> Predicate<T> for Contains<Substr>

Available on crate feature alloc only.
Source§

impl<T: AsRef<str>, Suffix: TypeString> Predicate<T> for EndsWith<Suffix>

Available on crate feature alloc only.