@@ -364,6 +364,8 @@ float_trait_impl!(Num for f32 f64);
364
364
/// If input is less than min then this returns min.
365
365
/// If input is greater than max then this returns max.
366
366
/// Otherwise this returns input.
367
+ ///
368
+ /// **Panics** in debug mode if `!(min <= max)`.
367
369
#[ inline]
368
370
pub fn clamp < T : PartialOrd > ( input : T , min : T , max : T ) -> T {
369
371
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 {
381
383
/// If input is less than min then this returns min.
382
384
/// Otherwise this returns input.
383
385
/// `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`.)
384
388
#[ inline]
385
389
pub fn clamp_min < T : PartialOrd > ( input : T , min : T ) -> T {
390
+ debug_assert ! ( min == min, "min must not be NAN" ) ;
386
391
if input < min {
387
392
min
388
393
} else {
@@ -395,8 +400,11 @@ pub fn clamp_min<T: PartialOrd>(input: T, min: T) -> T {
395
400
/// If input is greater than max then this returns max.
396
401
/// Otherwise this returns input.
397
402
/// `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`.)
398
405
#[ inline]
399
406
pub fn clamp_max < T : PartialOrd > ( input : T , max : T ) -> T {
407
+ debug_assert ! ( max == max, "max must not be NAN" ) ;
400
408
if input > max {
401
409
max
402
410
} else {
@@ -428,6 +436,41 @@ fn clamp_test() {
428
436
assert ! ( clamp_max( :: core:: f32 :: NAN , 1.0 ) . is_nan( ) ) ;
429
437
}
430
438
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
+
431
474
#[ test]
432
475
fn from_str_radix_unwrap ( ) {
433
476
// The Result error must impl Debug to allow unwrap()
0 commit comments