@@ -39,63 +39,55 @@ impl_float_to_int!(f32 => u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i12
3939impl_float_to_int ! ( f64 => u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ) ;
4040impl_float_to_int ! ( f128 => u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ) ;
4141
42- // Conversion traits for primitive integer and float types
43- // Conversions T -> T are covered by a blanket impl and therefore excluded
44- // Some conversions from and to usize/isize are not implemented due to portability concerns
42+ /// Implement `From<bool>` for integers
43+ macro_rules! impl_from_bool {
44+ ( $( $int: ty) * ) => { $(
45+ #[ stable( feature = "from_bool" , since = "1.28.0" ) ]
46+ #[ rustc_const_unstable( feature = "const_convert" , issue = "143773" ) ]
47+ impl const From <bool > for $int {
48+ /// Converts from [`bool`] to
49+ #[ doc = concat!( "[`" , stringify!( $int) , "`]" ) ]
50+ /// , by turning `false` into `0` and `true` into `1`.
51+ ///
52+ /// # Examples
53+ ///
54+ /// ```
55+ /// assert_eq!(
56+ #[ doc = stringify!( $int) ]
57+ /// ::from(false), 0);
58+ /// assert_eq!(
59+ #[ doc = stringify!( $int) ]
60+ /// ::from(true), 1);
61+ /// ```
62+ #[ inline( always) ]
63+ fn from( b: bool ) -> Self {
64+ b as Self
65+ }
66+ }
67+ ) * }
68+ }
69+
70+ // boolean -> integer
71+ impl_from_bool ! ( u8 u16 u32 u64 u128 usize ) ;
72+ impl_from_bool ! ( i8 i16 i32 i64 i128 isize ) ;
73+
74+ /// Implement `From<$small>` for `$large`
4575macro_rules! impl_from {
46- ( bool => $Int: ty $( , ) ?) => {
47- impl_from!(
48- bool => $Int,
49- #[ stable( feature = "from_bool" , since = "1.28.0" ) ] ,
50- concat!(
51- "Converts a [`bool`] to [`" , stringify!( $Int) , "`] losslessly.\n " ,
52- "The resulting value is `0` for `false` and `1` for `true` values.\n " ,
53- "\n " ,
54- "# Examples\n " ,
55- "\n " ,
56- "```\n " ,
57- "assert_eq!(" , stringify!( $Int) , "::from(true), 1);\n " ,
58- "assert_eq!(" , stringify!( $Int) , "::from(false), 0);\n " ,
59- "```\n " ,
60- ) ,
61- ) ;
62- } ;
63- ( $Small: ty => $Large: ty, #[ $attr: meta] $( , ) ?) => {
64- impl_from!(
65- $Small => $Large,
66- #[ $attr] ,
67- concat!( "Converts [`" , stringify!( $Small) , "`] to [`" , stringify!( $Large) , "`] losslessly." ) ,
68- ) ;
69- } ;
70- ( $Small: ty => $Large: ty, #[ $attr: meta] , $doc: expr $( , ) ?) => {
76+ ( $small: ty => $large: ty, #[ $attr: meta] ) => {
7177 #[ $attr]
7278 #[ rustc_const_unstable( feature = "const_convert" , issue = "143773" ) ]
73- impl const From <$Small> for $Large {
74- // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
75- // Rustdocs on functions do not.
76- #[ doc = $doc]
79+ impl const From <$small> for $large {
80+ #[ doc = concat!( "Converts from [`" , stringify!( $small) , "`] to [`" , stringify!( $large) , "`] losslessly." ) ]
7781 #[ inline( always) ]
78- fn from( small: $Small) -> Self {
82+ fn from( small: $small) -> Self {
83+ debug_assert!( <$large>:: MIN as i128 <= <$small>:: MIN as i128 ) ;
84+ debug_assert!( <$small>:: MAX as u128 <= <$large>:: MAX as u128 ) ;
7985 small as Self
8086 }
8187 }
82- } ;
88+ }
8389}
8490
85- // boolean -> integer
86- impl_from ! ( bool => u8 ) ;
87- impl_from ! ( bool => u16 ) ;
88- impl_from ! ( bool => u32 ) ;
89- impl_from ! ( bool => u64 ) ;
90- impl_from ! ( bool => u128 ) ;
91- impl_from ! ( bool => usize ) ;
92- impl_from ! ( bool => i8 ) ;
93- impl_from ! ( bool => i16 ) ;
94- impl_from ! ( bool => i32 ) ;
95- impl_from ! ( bool => i64 ) ;
96- impl_from ! ( bool => i128 ) ;
97- impl_from ! ( bool => isize ) ;
98-
9991// unsigned integer -> unsigned integer
10092impl_from ! ( u8 => u16 , #[ stable( feature = "lossless_int_conv" , since = "1.5.0" ) ] ) ;
10193impl_from ! ( u8 => u32 , #[ stable( feature = "lossless_int_conv" , since = "1.5.0" ) ] ) ;
0 commit comments