@@ -371,6 +371,43 @@ fn test_f32f64() {
371371 assert ! ( nan. is_nan( ) ) ;
372372}
373373
374+ /// Conversions where $source can be represented as bool.
375+ macro_rules! test_impl_try_from_integer_to_bool {
376+ ( $fn_name: ident, $source: ty) => {
377+ #[ test]
378+ fn $fn_name( ) {
379+ let max: $source = <$source>:: MAX ;
380+ let min: $source = <$source>:: MIN ;
381+ let zero: $source = 0 ;
382+ let one: $source = 1 ;
383+ let two: $source = 2 ;
384+ assert_eq!( bool :: try_from( max) . unwrap_err( ) . kind( ) , & IntErrorKind :: PosOverflow ) ;
385+ if min != 0 {
386+ assert_eq!( bool :: try_from( min) . unwrap_err( ) . kind( ) , & IntErrorKind :: NegOverflow ) ;
387+ assert_eq!(
388+ bool :: try_from( zero - 1 ) . unwrap_err( ) . kind( ) ,
389+ & IntErrorKind :: NegOverflow
390+ ) ;
391+ }
392+ assert_eq!( bool :: try_from( zero) . unwrap( ) , false ) ;
393+ assert_eq!( bool :: try_from( one) . unwrap( ) , true ) ;
394+ assert_eq!( bool :: try_from( two) . unwrap_err( ) . kind( ) , & IntErrorKind :: PosOverflow ) ;
395+ }
396+ } ;
397+ }
398+
399+ test_impl_try_from_integer_to_bool ! { test_try_u8bool, u8 }
400+ test_impl_try_from_integer_to_bool ! { test_try_u16bool, u16 }
401+ test_impl_try_from_integer_to_bool ! { test_try_u32bool, u32 }
402+ test_impl_try_from_integer_to_bool ! { test_try_u64bool, u64 }
403+ test_impl_try_from_integer_to_bool ! { test_try_u128bool, u128 }
404+
405+ test_impl_try_from_integer_to_bool ! { test_try_i8bool, i8 }
406+ test_impl_try_from_integer_to_bool ! { test_try_i16bool, i16 }
407+ test_impl_try_from_integer_to_bool ! { test_try_i32bool, i32 }
408+ test_impl_try_from_integer_to_bool ! { test_try_i64bool, i64 }
409+ test_impl_try_from_integer_to_bool ! { test_try_i128bool, i128 }
410+
374411/// Conversions where the full width of $source can be represented as $target
375412macro_rules! test_impl_try_from_always_ok {
376413 ( $fn_name: ident, $source: ty, $target: ty) => {
@@ -494,9 +531,15 @@ macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
494531 let zero: $source = 0 ;
495532 let neg_one: $source = -1 ;
496533 assert_eq!( <$target as TryFrom <$source>>:: try_from( max) . unwrap( ) , max as $target) ;
497- assert!( <$target as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
534+ assert_eq!(
535+ <$target as TryFrom <$source>>:: try_from( min) . unwrap_err( ) . kind( ) ,
536+ & IntErrorKind :: NegOverflow
537+ ) ;
498538 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
499- assert!( <$target as TryFrom <$source>>:: try_from( neg_one) . is_err( ) ) ;
539+ assert_eq!(
540+ <$target as TryFrom <$source>>:: try_from( neg_one) . unwrap_err( ) . kind( ) ,
541+ & IntErrorKind :: NegOverflow
542+ ) ;
500543 }
501544 } ;
502545}
@@ -557,7 +600,10 @@ macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
557600 let max = <$source>:: MAX ;
558601 let min = <$source>:: MIN ;
559602 let zero: $source = 0 ;
560- assert!( <$target as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
603+ assert_eq!(
604+ <$target as TryFrom <$source>>:: try_from( max) . unwrap_err( ) . kind( ) ,
605+ & IntErrorKind :: PosOverflow
606+ ) ;
561607 assert_eq!( <$target as TryFrom <$source>>:: try_from( min) . unwrap( ) , min as $target) ;
562608 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
563609 }
@@ -620,9 +666,15 @@ macro_rules! test_impl_try_from_same_sign_err {
620666 let zero: $source = 0 ;
621667 let t_max = <$target>:: MAX ;
622668 let t_min = <$target>:: MIN ;
623- assert!( <$target as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
669+ assert_eq!(
670+ <$target as TryFrom <$source>>:: try_from( max) . unwrap_err( ) . kind( ) ,
671+ & IntErrorKind :: PosOverflow
672+ ) ;
624673 if min != 0 {
625- assert!( <$target as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
674+ assert_eq!(
675+ <$target as TryFrom <$source>>:: try_from( min) . unwrap_err( ) . kind( ) ,
676+ & IntErrorKind :: NegOverflow
677+ ) ;
626678 }
627679 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
628680 assert_eq!(
@@ -709,8 +761,14 @@ macro_rules! test_impl_try_from_signed_to_unsigned_err {
709761 let zero: $source = 0 ;
710762 let t_max = <$target>:: MAX ;
711763 let t_min = <$target>:: MIN ;
712- assert!( <$target as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
713- assert!( <$target as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
764+ assert_eq!(
765+ <$target as TryFrom <$source>>:: try_from( max) . unwrap_err( ) . kind( ) ,
766+ & IntErrorKind :: PosOverflow
767+ ) ;
768+ assert_eq!(
769+ <$target as TryFrom <$source>>:: try_from( min) . unwrap_err( ) . kind( ) ,
770+ & IntErrorKind :: NegOverflow
771+ ) ;
714772 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
715773 assert_eq!(
716774 <$target as TryFrom <$source>>:: try_from( t_max as $source) . unwrap( ) ,
0 commit comments