1- use crate :: num:: TryFromIntError ;
1+ use crate :: num:: { IntErrorKind , TryFromIntError } ;
22
33mod private {
44 /// This trait being unreachable from outside the crate
@@ -274,7 +274,7 @@ macro_rules! impl_try_from_lower_bounded {
274274 if u >= 0 {
275275 Ok ( u as Self )
276276 } else {
277- Err ( TryFromIntError ( ( ) ) )
277+ Err ( TryFromIntError ( IntErrorKind :: NegOverflow ) )
278278 }
279279 }
280280 }
@@ -295,7 +295,7 @@ macro_rules! impl_try_from_upper_bounded {
295295 #[ inline]
296296 fn try_from( u: $source) -> Result <Self , Self :: Error > {
297297 if u > ( Self :: MAX as $source) {
298- Err ( TryFromIntError ( ( ) ) )
298+ Err ( TryFromIntError ( IntErrorKind :: PosOverflow ) )
299299 } else {
300300 Ok ( u as Self )
301301 }
@@ -319,8 +319,10 @@ macro_rules! impl_try_from_both_bounded {
319319 fn try_from( u: $source) -> Result <Self , Self :: Error > {
320320 let min = Self :: MIN as $source;
321321 let max = Self :: MAX as $source;
322- if u < min || u > max {
323- Err ( TryFromIntError ( ( ) ) )
322+ if u < min {
323+ Err ( TryFromIntError ( IntErrorKind :: NegOverflow ) )
324+ } else if u > max {
325+ Err ( TryFromIntError ( IntErrorKind :: PosOverflow ) )
324326 } else {
325327 Ok ( u as Self )
326328 }
@@ -329,8 +331,8 @@ macro_rules! impl_try_from_both_bounded {
329331 ) * }
330332}
331333
332- /// Implement `TryFrom<integer >` for `bool`
333- macro_rules! impl_try_from_integer_for_bool {
334+ /// Implement `TryFrom<unsigned_integer >` for `bool`
335+ macro_rules! impl_try_from_unsigned_integer_for_bool {
334336 ( $( $int: ty) +) => { $(
335337 #[ stable( feature = "try_from" , since = "1.34.0" ) ]
336338 #[ rustc_const_unstable( feature = "const_convert" , issue = "143773" ) ]
@@ -354,7 +356,40 @@ macro_rules! impl_try_from_integer_for_bool {
354356 match i {
355357 0 => Ok ( false ) ,
356358 1 => Ok ( true ) ,
357- _ => Err ( TryFromIntError ( ( ) ) ) ,
359+ 2 .. => Err ( TryFromIntError ( IntErrorKind :: PosOverflow ) ) ,
360+ }
361+ }
362+ }
363+ ) * }
364+ }
365+
366+ /// Implement `TryFrom<signed_integer>` for `bool`
367+ macro_rules! impl_try_from_signed_integer_for_bool {
368+ ( $( $int: ty) +) => { $(
369+ #[ stable( feature = "try_from" , since = "1.34.0" ) ]
370+ #[ rustc_const_unstable( feature = "const_convert" , issue = "143773" ) ]
371+ impl const TryFrom <$int> for bool {
372+ type Error = TryFromIntError ;
373+
374+ /// Tries to create a bool from an integer type.
375+ /// Returns an error if the integer is not 0 or 1.
376+ ///
377+ /// # Examples
378+ ///
379+ /// ```
380+ #[ doc = concat!( "assert_eq!(0_" , stringify!( $int) , ".try_into(), Ok(false));" ) ]
381+ ///
382+ #[ doc = concat!( "assert_eq!(1_" , stringify!( $int) , ".try_into(), Ok(true));" ) ]
383+ ///
384+ #[ doc = concat!( "assert!(<" , stringify!( $int) , " as TryInto<bool>>::try_into(2).is_err());" ) ]
385+ /// ```
386+ #[ inline]
387+ fn try_from( i: $int) -> Result <Self , Self :: Error > {
388+ match i {
389+ 0 => Ok ( false ) ,
390+ 1 => Ok ( true ) ,
391+ ..0 => Err ( TryFromIntError ( IntErrorKind :: NegOverflow ) ) ,
392+ 2 .. => Err ( TryFromIntError ( IntErrorKind :: PosOverflow ) ) ,
358393 }
359394 }
360395 }
@@ -368,8 +403,8 @@ macro_rules! rev {
368403}
369404
370405// integer -> bool
371- impl_try_from_integer_for_bool ! ( u128 u64 u32 u16 u8 ) ;
372- impl_try_from_integer_for_bool ! ( i128 i64 i32 i16 i8 ) ;
406+ impl_try_from_unsigned_integer_for_bool ! ( u128 u64 u32 u16 u8 ) ;
407+ impl_try_from_signed_integer_for_bool ! ( i128 i64 i32 i16 i8 ) ;
373408
374409// unsigned integer -> unsigned integer
375410impl_try_from_upper_bounded ! ( u16 => u8 ) ;
@@ -407,7 +442,7 @@ impl_try_from_lower_bounded!(isize => usize);
407442
408443#[ cfg( target_pointer_width = "16" ) ]
409444mod ptr_try_from_impls {
410- use super :: TryFromIntError ;
445+ use super :: { IntErrorKind , TryFromIntError } ;
411446
412447 impl_try_from_upper_bounded ! ( usize => u8 ) ;
413448 impl_try_from_unbounded ! ( usize => u16 , u32 , u64 , u128 ) ;
@@ -429,7 +464,7 @@ mod ptr_try_from_impls {
429464
430465#[ cfg( target_pointer_width = "32" ) ]
431466mod ptr_try_from_impls {
432- use super :: TryFromIntError ;
467+ use super :: { IntErrorKind , TryFromIntError } ;
433468
434469 impl_try_from_upper_bounded ! ( usize => u8 , u16 ) ;
435470 impl_try_from_unbounded ! ( usize => u32 , u64 , u128 ) ;
@@ -454,7 +489,7 @@ mod ptr_try_from_impls {
454489
455490#[ cfg( target_pointer_width = "64" ) ]
456491mod ptr_try_from_impls {
457- use super :: TryFromIntError ;
492+ use super :: { IntErrorKind , TryFromIntError } ;
458493
459494 impl_try_from_upper_bounded ! ( usize => u8 , u16 , u32 ) ;
460495 impl_try_from_unbounded ! ( usize => u64 , u128 ) ;
@@ -552,7 +587,7 @@ macro_rules! impl_nonzero_int_try_from_int {
552587 #[ doc = concat!( "to <code>[NonZero]\\ <[" , stringify!( $Int) , "]></code>." ) ]
553588 #[ inline]
554589 fn try_from( value: $Int) -> Result <Self , Self :: Error > {
555- Self :: new( value) . ok_or( TryFromIntError ( ( ) ) )
590+ Self :: new( value) . ok_or( TryFromIntError ( IntErrorKind :: Zero ) )
556591 }
557592 }
558593 } ;
0 commit comments