@@ -39,63 +39,51 @@ 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!(Self::from(false), 0);
56+ /// assert_eq!(Self::from(true), 1);
57+ /// ```
58+ #[ inline( always) ]
59+ fn from( b: bool ) -> Self {
60+ b as Self
61+ }
62+ }
63+ ) * }
64+ }
65+
66+ // boolean -> integer
67+ impl_from_bool ! ( u8 u16 u32 u64 u128 usize ) ;
68+ impl_from_bool ! ( i8 i16 i32 i64 i128 isize ) ;
69+
70+ /// Implement `From<$small>` for `$large`
4571macro_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 $( , ) ?) => {
72+ ( $small: ty => $large: ty, #[ $attr: meta] ) => {
7173 #[ $attr]
7274 #[ 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]
75+ impl const From <$small> for $large {
76+ #[ doc = concat!( "Converts from [`" , stringify!( $small) , "`] to [`" , stringify!( $large) , "`] losslessly." ) , ]
7777 #[ inline( always) ]
78- fn from( small: $Small) -> Self {
78+ fn from( small: $small) -> Self {
79+ debug_assert!( $large:: MIN <= $small:: MIN ) ;
80+ debug_assert!( $small:: MAX <= $large:: MAX ) ;
7981 small as Self
8082 }
8183 }
82- } ;
84+ }
8385}
8486
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-
9987// unsigned integer -> unsigned integer
10088impl_from ! ( u8 => u16 , #[ stable( feature = "lossless_int_conv" , since = "1.5.0" ) ] ) ;
10189impl_from ! ( u8 => u32 , #[ stable( feature = "lossless_int_conv" , since = "1.5.0" ) ] ) ;
0 commit comments