Skip to content

Commit d15a98b

Browse files
committed
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 (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 parent 03d2f5c commit d15a98b

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
#![feature(rtm_target_feature)]
132132
#![feature(f16c_target_feature)]
133133
#![feature(hexagon_target_feature)]
134-
#![feature(const_int_conversion)]
135134
#![feature(const_transmute)]
136135
#![feature(structural_match)]
137136
#![feature(abi_unadjusted)]

src/libcore/num/mod.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
21952195
assert_eq!(bytes, ", $be_bytes, ");
21962196
```"),
21972197
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2198-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2198+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
21992199
#[inline]
22002200
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
22012201
self.to_be().to_ne_bytes()
@@ -2215,7 +2215,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
22152215
assert_eq!(bytes, ", $le_bytes, ");
22162216
```"),
22172217
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2218-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2218+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
22192219
#[inline]
22202220
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
22212221
self.to_le().to_ne_bytes()
@@ -2250,7 +2250,8 @@ assert_eq!(
22502250
);
22512251
```"),
22522252
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2253-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2253+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
2254+
#[allow_internal_unstable(const_transmute)]
22542255
#[inline]
22552256
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
22562257
// SAFETY: integers are plain old datatypes so we can always transmute them to
@@ -2284,7 +2285,7 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
22842285
}
22852286
```"),
22862287
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2287-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2288+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
22882289
#[inline]
22892290
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
22902291
Self::from_be(Self::from_ne_bytes(bytes))
@@ -2317,7 +2318,7 @@ fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
23172318
}
23182319
```"),
23192320
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2320-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2321+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
23212322
#[inline]
23222323
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
23232324
Self::from_le(Self::from_ne_bytes(bytes))
@@ -2360,7 +2361,8 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
23602361
}
23612362
```"),
23622363
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2363-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2364+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
2365+
#[allow_internal_unstable(const_transmute)]
23642366
#[inline]
23652367
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
23662368
// SAFETY: integers are plain old datatypes so we can always transmute to them
@@ -4132,7 +4134,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
41324134
assert_eq!(bytes, ", $be_bytes, ");
41334135
```"),
41344136
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4135-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4137+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
41364138
#[inline]
41374139
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
41384140
self.to_be().to_ne_bytes()
@@ -4152,7 +4154,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
41524154
assert_eq!(bytes, ", $le_bytes, ");
41534155
```"),
41544156
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4155-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4157+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
41564158
#[inline]
41574159
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
41584160
self.to_le().to_ne_bytes()
@@ -4187,7 +4189,8 @@ assert_eq!(
41874189
);
41884190
```"),
41894191
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4190-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4192+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
4193+
#[allow_internal_unstable(const_transmute)]
41914194
#[inline]
41924195
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
41934196
// SAFETY: integers are plain old datatypes so we can always transmute them to
@@ -4221,7 +4224,7 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
42214224
}
42224225
```"),
42234226
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4224-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4227+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
42254228
#[inline]
42264229
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
42274230
Self::from_be(Self::from_ne_bytes(bytes))
@@ -4254,7 +4257,7 @@ fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
42544257
}
42554258
```"),
42564259
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4257-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4260+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
42584261
#[inline]
42594262
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
42604263
Self::from_le(Self::from_ne_bytes(bytes))
@@ -4297,7 +4300,8 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
42974300
}
42984301
```"),
42994302
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4300-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4303+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
4304+
#[allow_internal_unstable(const_transmute)]
43014305
#[inline]
43024306
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
43034307
// SAFETY: integers are plain old datatypes so we can always transmute to them

src/test/ui/consts/const-int-conversion-rpass.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// run-pass
22

3-
#![feature(const_int_conversion)]
4-
53
const REVERSE: u32 = 0x12345678_u32.reverse_bits();
64
const FROM_BE_BYTES: i32 = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
75
const FROM_LE_BYTES: i32 = i32::from_le_bytes([0x12, 0x34, 0x56, 0x78]);

0 commit comments

Comments
 (0)