Trait decorum::cmp::IntrinsicOrd
source · [−]pub trait IntrinsicOrd: Copy + PartialOrd + Sized {
fn is_undefined(&self) -> bool;
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self);
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 NaN
s 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 NaN
s 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
fn is_undefined(&self) -> bool
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.
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self)
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());