@@ -374,6 +374,43 @@ fn test_f32f64() {
374374 assert ! ( nan. is_nan( ) ) ;
375375}
376376
377+ /// Conversions where $source can be represented as bool.
378+ macro_rules! test_impl_try_from_integer_to_bool {
379+ ( $fn_name: ident, $source: ty) => {
380+ #[ test]
381+ fn $fn_name( ) {
382+ let max: $source = <$source>:: MAX ;
383+ let min: $source = <$source>:: MIN ;
384+ let zero: $source = 0 ;
385+ let one: $source = 1 ;
386+ let two: $source = 2 ;
387+ assert_eq!( bool :: try_from( max) . unwrap_err( ) . kind( ) , & IntErrorKind :: PosOverflow ) ;
388+ if min != 0 {
389+ assert_eq!( bool :: try_from( min) . unwrap_err( ) . kind( ) , & IntErrorKind :: NegOverflow ) ;
390+ assert_eq!(
391+ bool :: try_from( zero - 1 ) . unwrap_err( ) . kind( ) ,
392+ & IntErrorKind :: NegOverflow
393+ ) ;
394+ }
395+ assert_eq!( bool :: try_from( zero) . unwrap( ) , false ) ;
396+ assert_eq!( bool :: try_from( one) . unwrap( ) , true ) ;
397+ assert_eq!( bool :: try_from( two) . unwrap_err( ) . kind( ) , & IntErrorKind :: PosOverflow ) ;
398+ }
399+ } ;
400+ }
401+
402+ test_impl_try_from_integer_to_bool ! { test_try_u8bool, u8 }
403+ test_impl_try_from_integer_to_bool ! { test_try_u16bool, u16 }
404+ test_impl_try_from_integer_to_bool ! { test_try_u32bool, u32 }
405+ test_impl_try_from_integer_to_bool ! { test_try_u64bool, u64 }
406+ test_impl_try_from_integer_to_bool ! { test_try_u128bool, u128 }
407+
408+ test_impl_try_from_integer_to_bool ! { test_try_i8bool, i8 }
409+ test_impl_try_from_integer_to_bool ! { test_try_i16bool, i16 }
410+ test_impl_try_from_integer_to_bool ! { test_try_i32bool, i32 }
411+ test_impl_try_from_integer_to_bool ! { test_try_i64bool, i64 }
412+ test_impl_try_from_integer_to_bool ! { test_try_i128bool, i128 }
413+
377414/// Conversions where the full width of $source can be represented as $target
378415macro_rules! test_impl_try_from_always_ok {
379416 ( $fn_name: ident, $source: ty, $target: ty) => {
@@ -497,9 +534,15 @@ macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
497534 let zero: $source = 0 ;
498535 let neg_one: $source = -1 ;
499536 assert_eq!( <$target as TryFrom <$source>>:: try_from( max) . unwrap( ) , max as $target) ;
500- assert!( <$target as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
537+ assert_eq!(
538+ <$target as TryFrom <$source>>:: try_from( min) . unwrap_err( ) . kind( ) ,
539+ & IntErrorKind :: NegOverflow
540+ ) ;
501541 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
502- assert!( <$target as TryFrom <$source>>:: try_from( neg_one) . is_err( ) ) ;
542+ assert_eq!(
543+ <$target as TryFrom <$source>>:: try_from( neg_one) . unwrap_err( ) . kind( ) ,
544+ & IntErrorKind :: NegOverflow
545+ ) ;
503546 }
504547 } ;
505548}
@@ -560,7 +603,10 @@ macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
560603 let max = <$source>:: MAX ;
561604 let min = <$source>:: MIN ;
562605 let zero: $source = 0 ;
563- assert!( <$target as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
606+ assert_eq!(
607+ <$target as TryFrom <$source>>:: try_from( max) . unwrap_err( ) . kind( ) ,
608+ & IntErrorKind :: PosOverflow
609+ ) ;
564610 assert_eq!( <$target as TryFrom <$source>>:: try_from( min) . unwrap( ) , min as $target) ;
565611 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
566612 }
@@ -623,9 +669,15 @@ macro_rules! test_impl_try_from_same_sign_err {
623669 let zero: $source = 0 ;
624670 let t_max = <$target>:: MAX ;
625671 let t_min = <$target>:: MIN ;
626- assert!( <$target as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
672+ assert_eq!(
673+ <$target as TryFrom <$source>>:: try_from( max) . unwrap_err( ) . kind( ) ,
674+ & IntErrorKind :: PosOverflow
675+ ) ;
627676 if min != 0 {
628- assert!( <$target as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
677+ assert_eq!(
678+ <$target as TryFrom <$source>>:: try_from( min) . unwrap_err( ) . kind( ) ,
679+ & IntErrorKind :: NegOverflow
680+ ) ;
629681 }
630682 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
631683 assert_eq!(
@@ -712,8 +764,14 @@ macro_rules! test_impl_try_from_signed_to_unsigned_err {
712764 let zero: $source = 0 ;
713765 let t_max = <$target>:: MAX ;
714766 let t_min = <$target>:: MIN ;
715- assert!( <$target as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
716- assert!( <$target as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
767+ assert_eq!(
768+ <$target as TryFrom <$source>>:: try_from( max) . unwrap_err( ) . kind( ) ,
769+ & IntErrorKind :: PosOverflow
770+ ) ;
771+ assert_eq!(
772+ <$target as TryFrom <$source>>:: try_from( min) . unwrap_err( ) . kind( ) ,
773+ & IntErrorKind :: NegOverflow
774+ ) ;
717775 assert_eq!( <$target as TryFrom <$source>>:: try_from( zero) . unwrap( ) , zero as $target) ;
718776 assert_eq!(
719777 <$target as TryFrom <$source>>:: try_from( t_max as $source) . unwrap( ) ,
0 commit comments