decorum::cmp

Trait IntrinsicOrd

Source
pub trait IntrinsicOrd:
    Copy
    + PartialOrd
    + Sized {
    // Required methods
    fn is_undefined(&self) -> bool;
    fn min_max_or_undefined(&self, other: &Self) -> (Self, Self);

    // Provided methods
    fn min_or_undefined(&self, other: &Self) -> Self { ... }
    fn max_or_undefined(&self, other: &Self) -> Self { ... }
}
Expand description

Partial ordering of types with intrinsic representations for undefined comparisons.

IntrinsicOrd is similar to PartialOrd, but provides a pairwise minimum-maximum API and, for types without a total ordering, is only implemented for such types that additionally have intrinsic representations for undefined, such as the None variant of Option and NaNs for floating-point primitives. PrimitiveOrd is also closed and always compares two values of the same type.

This trait is also implemented for numeric types with total orderings, and can be used for comparisons that propagate NaNs for floating-point primitives (unlike PartialOrd, which expresses comparisons of types T and U with the extrinsic type Option<Ordering>).

See the min_or_undefined and max_or_undefined functions.

Required Methods§

Source

fn is_undefined(&self) -> bool

Returns true if a value encodes undefined, otherwise false.

Prefer this predicate over direct comparisons. For floating-point representations, NaN is considered undefined, but direct comparisons with NaN values should be avoided.

Source

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Compares two values and returns their pairwise minimum and maximum.

This function returns a representation of undefined for both the minimum and maximum if either of the inputs are undefined or the inputs cannot be compared, even if undefined values are ordered or the type has a total ordering. Undefined values are always propagated.

§Examples

Propagating NaN values when comparing proxy types with a total ordering:

use decorum::cmp::{self, IntrinsicOrd};
use decorum::{Nan, Total};

let x: Total<f64> = 0.0.into();
let y: Total<f64> = (0.0 / 0.0).into(); // `NaN`.

// `Total` provides a total ordering in which zero is less than `NaN`, but `NaN`
// is considered undefined and is the result of the intrinsic comparison.
assert!(y.is_undefined());
assert!(cmp::min_or_undefined(x, y).is_undefined());

Provided Methods§

Source

fn min_or_undefined(&self, other: &Self) -> Self

Source

fn max_or_undefined(&self, other: &Self) -> Self

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.

Implementations on Foreign Types§

Source§

impl IntrinsicOrd for f32

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for f64

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for i8

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for i16

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for i32

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for i64

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for i128

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for isize

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for u8

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for u16

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for u32

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for u64

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for u128

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl IntrinsicOrd for usize

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Source§

impl<T> IntrinsicOrd for Option<T>
where T: Copy + PartialOrd,

Source§

fn is_undefined(&self) -> bool

Source§

fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)

Implementors§

Source§

impl<T, P> IntrinsicOrd for ConstrainedFloat<T, P>
where T: Float + IntrinsicOrd + Primitive, P: Constraint<T>,