pub trait LimitFloat: Sized {
// Provided method
fn limit<B: LimitFloatBounds<Self>>(self, bounds: B) -> Self { ... }
}Expand description
Adds the limit method for floating point types.
This is implemented on f32, f64, as well as f16 and f128 if the nightly feature is enabled.
Note that this behaves like clamp, not min/max. Specifically, if self is NaN, the result is NaN. NaN bounds are not supported and will panic.
§Panics
Panics if any bound is NaN, or if the start bound is greater than the end bound.
§Example
assert_eq!(5.0f32.limit(3.0..), 5.0);
assert_eq!(2.0f32.limit(3.0..), 3.0);
assert!(f32::NAN.limit(3.0..).is_nan());
assert_eq!(5.0f32.limit(..=7.0), 5.0);
assert_eq!(9.0f32.limit(..=7.0), 7.0);
assert!(f32::NAN.limit(..=7.0).is_nan());
assert_eq!(5.0f32.limit(3.0..=7.0), 5.0);
assert_eq!(2.0f32.limit(3.0..=7.0), 3.0);
assert_eq!(9.0f32.limit(3.0..=7.0), 7.0);
assert!(f32::NAN.limit(3.0..=7.0).is_nan());
assert_eq!(5.0f32.limit(..), 5.0);
assert_eq!(f32::INFINITY.limit(..), f32::INFINITY);Provided Methods§
fn limit<B: LimitFloatBounds<Self>>(self, bounds: B) -> 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.