Skip to content

Commit d394467

Browse files
Merge #135
135: Debug-panic in clamp_min/max if min/max is NAN r=cuviper a=jturner314 This also improves the docs for `clamp`, `clamp_min`, and `clamp_max`. Closes #134. Co-authored-by: Jim Turner <[email protected]>
2 parents 428f89a + 987ed8f commit d394467

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ float_trait_impl!(Num for f32 f64);
364364
/// If input is less than min then this returns min.
365365
/// If input is greater than max then this returns max.
366366
/// Otherwise this returns input.
367+
///
368+
/// **Panics** in debug mode if `!(min <= max)`.
367369
#[inline]
368370
pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
369371
debug_assert!(min <= max, "min must be less than or equal to max");
@@ -381,8 +383,11 @@ pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
381383
/// If input is less than min then this returns min.
382384
/// Otherwise this returns input.
383385
/// `clamp_min(std::f32::NAN, 1.0)` preserves `NAN` different from `f32::min(std::f32::NAN, 1.0)`.
386+
///
387+
/// **Panics** in debug mode if `!(min == min)`. (This occurs if `min` is `NAN`.)
384388
#[inline]
385389
pub fn clamp_min<T: PartialOrd>(input: T, min: T) -> T {
390+
debug_assert!(min == min, "min must not be NAN");
386391
if input < min {
387392
min
388393
} else {
@@ -395,8 +400,11 @@ pub fn clamp_min<T: PartialOrd>(input: T, min: T) -> T {
395400
/// If input is greater than max then this returns max.
396401
/// Otherwise this returns input.
397402
/// `clamp_max(std::f32::NAN, 1.0)` preserves `NAN` different from `f32::max(std::f32::NAN, 1.0)`.
403+
///
404+
/// **Panics** in debug mode if `!(max == max)`. (This occurs if `max` is `NAN`.)
398405
#[inline]
399406
pub fn clamp_max<T: PartialOrd>(input: T, max: T) -> T {
407+
debug_assert!(max == max, "max must not be NAN");
400408
if input > max {
401409
max
402410
} else {
@@ -428,6 +436,41 @@ fn clamp_test() {
428436
assert!(clamp_max(::core::f32::NAN, 1.0).is_nan());
429437
}
430438

439+
#[test]
440+
#[should_panic]
441+
#[cfg(debug_assertions)]
442+
fn clamp_nan_min() {
443+
clamp(0., ::core::f32::NAN, 1.);
444+
}
445+
446+
#[test]
447+
#[should_panic]
448+
#[cfg(debug_assertions)]
449+
fn clamp_nan_max() {
450+
clamp(0., -1., ::core::f32::NAN);
451+
}
452+
453+
#[test]
454+
#[should_panic]
455+
#[cfg(debug_assertions)]
456+
fn clamp_nan_min_max() {
457+
clamp(0., ::core::f32::NAN, ::core::f32::NAN);
458+
}
459+
460+
#[test]
461+
#[should_panic]
462+
#[cfg(debug_assertions)]
463+
fn clamp_min_nan_min() {
464+
clamp_min(0., ::core::f32::NAN);
465+
}
466+
467+
#[test]
468+
#[should_panic]
469+
#[cfg(debug_assertions)]
470+
fn clamp_max_nan_max() {
471+
clamp_max(0., ::core::f32::NAN);
472+
}
473+
431474
#[test]
432475
fn from_str_radix_unwrap() {
433476
// The Result error must impl Debug to allow unwrap()

0 commit comments

Comments
 (0)