]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #69373 - tspiteri:const_int_conversion, r=oli-obk
authorMazdak Farrokhzad <twingoow@gmail.com>
Wed, 11 Mar 2020 09:36:18 +0000 (10:36 +0100)
committerGitHub <noreply@github.com>
Wed, 11 Mar 2020 09:36:18 +0000 (10:36 +0100)
Stabilize const for integer {to,from}_{be,le,ne}_bytes methods

All of these functions can be implemented simply and naturally as const functions, e.g. `u32::from_le_bytes` can be implemented as
```rust
(bytes[0] as u32)
    | (bytes[1] as u32) << 8
    | (bytes[2] as u32) << 16
    | (bytes[3] as u32) << 24
```
So stabilizing the constness will not expose that internally they are implemented using transmute which is not const in stable.

1  2 
src/libcore/lib.rs
src/libcore/num/mod.rs

Simple merge
index 889ce2f211e14229f079e477e97207cb1ba9e24f,b64abc319360eb27b903631dca70977a4996c33a..caffa6c509aaa5071dcfd1ce9c9249a42c73c64f
@@@ -2319,45 -2368,21 +2327,53 @@@ fn read_ne_", stringify!($SelfT), "(inp
  }
  ```"),
              #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
-             #[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
+             #[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
+             // SAFETY: const sound because integers are plain old datatypes so we can always
+             // transmute to them
+             #[allow_internal_unstable(const_fn_union)]
              #[inline]
              pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
+                 #[repr(C)]
+                 union Bytes {
+                     val: $SelfT,
+                     bytes: [u8; mem::size_of::<$SelfT>()],
+                 }
                  // SAFETY: integers are plain old datatypes so we can always transmute to them
-                 unsafe { mem::transmute(bytes) }
+                 unsafe { Bytes { bytes }.val }
              }
          }
 +
 +        doc_comment! {
 +            concat!("**This method is soft-deprecated.**
 +
 +Although using it won’t cause compilation warning,
 +new code should use [`", stringify!($SelfT), "::MIN", "`](#associatedconstant.MIN) instead.
 +
 +Returns the smallest value that can be represented by this integer type."),
 +            #[stable(feature = "rust1", since = "1.0.0")]
 +            #[inline(always)]
 +            #[rustc_promotable]
 +            #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
 +            pub const fn min_value() -> Self {
 +                Self::MIN
 +            }
 +        }
 +
 +        doc_comment! {
 +            concat!("**This method is soft-deprecated.**
 +
 +Although using it won’t cause compilation warning,
 +new code should use [`", stringify!($SelfT), "::MAX", "`](#associatedconstant.MAX) instead.
 +
 +Returns the largest value that can be represented by this integer type."),
 +            #[stable(feature = "rust1", since = "1.0.0")]
 +            #[inline(always)]
 +            #[rustc_promotable]
 +            #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
 +            pub const fn max_value() -> Self {
 +                Self::MAX
 +            }
 +        }
      }
  }
  
@@@ -4264,41 -4321,21 +4288,49 @@@ fn read_ne_", stringify!($SelfT), "(inp
  }
  ```"),
              #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
-             #[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
+             #[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
+             // SAFETY: const sound because integers are plain old datatypes so we can always
+             // transmute to them
+             #[allow_internal_unstable(const_fn_union)]
              #[inline]
              pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
+                 #[repr(C)]
+                 union Bytes {
+                     val: $SelfT,
+                     bytes: [u8; mem::size_of::<$SelfT>()],
+                 }
                  // SAFETY: integers are plain old datatypes so we can always transmute to them
-                 unsafe { mem::transmute(bytes) }
+                 unsafe { Bytes { bytes }.val }
              }
          }
 +
 +        doc_comment! {
 +            concat!("**This method is soft-deprecated.**
 +
 +Although using it won’t cause compilation warning,
 +new code should use [`", stringify!($SelfT), "::MIN", "`](#associatedconstant.MIN) instead.
 +
 +Returns the smallest value that can be represented by this integer type."),
 +            #[stable(feature = "rust1", since = "1.0.0")]
 +            #[rustc_promotable]
 +            #[inline(always)]
 +            #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
 +            pub const fn min_value() -> Self { Self::MIN }
 +        }
 +
 +        doc_comment! {
 +            concat!("**This method is soft-deprecated.**
 +
 +Although using it won’t cause compilation warning,
 +new code should use [`", stringify!($SelfT), "::MAX", "`](#associatedconstant.MAX) instead.
 +
 +Returns the largest value that can be represented by this integer type."),
 +            #[stable(feature = "rust1", since = "1.0.0")]
 +            #[rustc_promotable]
 +            #[inline(always)]
 +            #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
 +            pub const fn max_value() -> Self { Self::MAX }
 +        }
      }
  }