pub trait Max: PartialOrd + Sized {
// Provided methods
fn max<'a>(&'a self, other: &'a Self) -> &'a Self { ... }
fn max_by_value(self, other: Self) -> Self { ... }
}Expand description
A trait for finding the maximum of two values.
This trait provides a max method that returns a reference to the greater of
two values. It is bounded by PartialOrd, which allows it to handle types
with partial ordering.
§Implementations
- For types that already guarantee validity, like
RealValidated, the default implementation is sufficient and safe becausePartialOrdis already correctly implemented. - A specialized implementation is provided for
f64to add validation checks in debug builds, ensuring inputs are finite and normal, and panicking if they are not.
§Behavior with NaN
For f64, if either input is NaN, the comparison self >= other
is false, which means other will be returned. Note that in debug builds,
providing a NaN input will cause a panic due to the validation checks.
RealValidated types should not contain NaN values by contract.
Provided Methods§
Sourcefn max<'a>(&'a self, other: &'a Self) -> &'a Self
fn max<'a>(&'a self, other: &'a Self) -> &'a Self
Returns a reference to the maximum of self and other.
§Examples
use num_valid::functions::Max;
assert_eq!(Max::max(&5.0, &3.0), &5.0);
assert_eq!(Max::max(&-10.0, &-5.0), &-5.0);Sourcefn max_by_value(self, other: Self) -> Self
fn max_by_value(self, other: Self) -> Self
Returns the maximum of self and other, consuming both values.
§Examples
use num_valid::functions::Max;
assert_eq!(5.0f64.max_by_value(3.0), 5.0);
assert_eq!((-10.0f64).max_by_value(-5.0), -5.0);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.