Skip to content

Commit 4307914

Browse files
authored
Rollup merge of #69373 - tspiteri:const_int_conversion, r=oli-obk
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.
2 parents a7c2eef + 87f0dc6 commit 4307914

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@
130130
#![feature(rtm_target_feature)]
131131
#![feature(f16c_target_feature)]
132132
#![feature(hexagon_target_feature)]
133-
#![feature(const_int_conversion)]
134133
#![feature(const_transmute)]
135134
#![feature(structural_match)]
136135
#![feature(abi_unadjusted)]

src/libcore/num/mod.rs

+48-16
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
21542154
assert_eq!(bytes, ", $be_bytes, ");
21552155
```"),
21562156
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2157-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2157+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
21582158
#[inline]
21592159
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
21602160
self.to_be().to_ne_bytes()
@@ -2174,7 +2174,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
21742174
assert_eq!(bytes, ", $le_bytes, ");
21752175
```"),
21762176
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2177-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2177+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
21782178
#[inline]
21792179
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
21802180
self.to_le().to_ne_bytes()
@@ -2209,12 +2209,20 @@ assert_eq!(
22092209
);
22102210
```"),
22112211
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2212-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2212+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
2213+
// SAFETY: const sound because integers are plain old datatypes so we can always
2214+
// transmute them to arrays of bytes
2215+
#[allow_internal_unstable(const_fn_union)]
22132216
#[inline]
22142217
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
2218+
#[repr(C)]
2219+
union Bytes {
2220+
val: $SelfT,
2221+
bytes: [u8; mem::size_of::<$SelfT>()],
2222+
}
22152223
// SAFETY: integers are plain old datatypes so we can always transmute them to
22162224
// arrays of bytes
2217-
unsafe { mem::transmute(self) }
2225+
unsafe { Bytes { val: self }.bytes }
22182226
}
22192227
}
22202228

@@ -2243,7 +2251,7 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
22432251
}
22442252
```"),
22452253
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2246-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2254+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
22472255
#[inline]
22482256
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
22492257
Self::from_be(Self::from_ne_bytes(bytes))
@@ -2276,7 +2284,7 @@ fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
22762284
}
22772285
```"),
22782286
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2279-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2287+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
22802288
#[inline]
22812289
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
22822290
Self::from_le(Self::from_ne_bytes(bytes))
@@ -2319,11 +2327,19 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
23192327
}
23202328
```"),
23212329
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2322-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
2330+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
2331+
// SAFETY: const sound because integers are plain old datatypes so we can always
2332+
// transmute to them
2333+
#[allow_internal_unstable(const_fn_union)]
23232334
#[inline]
23242335
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2336+
#[repr(C)]
2337+
union Bytes {
2338+
val: $SelfT,
2339+
bytes: [u8; mem::size_of::<$SelfT>()],
2340+
}
23252341
// SAFETY: integers are plain old datatypes so we can always transmute to them
2326-
unsafe { mem::transmute(bytes) }
2342+
unsafe { Bytes { bytes }.val }
23272343
}
23282344
}
23292345

@@ -4099,7 +4115,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();
40994115
assert_eq!(bytes, ", $be_bytes, ");
41004116
```"),
41014117
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4102-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4118+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
41034119
#[inline]
41044120
pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
41054121
self.to_be().to_ne_bytes()
@@ -4119,7 +4135,7 @@ let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();
41194135
assert_eq!(bytes, ", $le_bytes, ");
41204136
```"),
41214137
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4122-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4138+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
41234139
#[inline]
41244140
pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
41254141
self.to_le().to_ne_bytes()
@@ -4154,12 +4170,20 @@ assert_eq!(
41544170
);
41554171
```"),
41564172
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4157-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4173+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
4174+
// SAFETY: const sound because integers are plain old datatypes so we can always
4175+
// transmute them to arrays of bytes
4176+
#[allow_internal_unstable(const_fn_union)]
41584177
#[inline]
41594178
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
4179+
#[repr(C)]
4180+
union Bytes {
4181+
val: $SelfT,
4182+
bytes: [u8; mem::size_of::<$SelfT>()],
4183+
}
41604184
// SAFETY: integers are plain old datatypes so we can always transmute them to
41614185
// arrays of bytes
4162-
unsafe { mem::transmute(self) }
4186+
unsafe { Bytes { val: self }.bytes }
41634187
}
41644188
}
41654189

@@ -4188,7 +4212,7 @@ fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
41884212
}
41894213
```"),
41904214
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4191-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4215+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
41924216
#[inline]
41934217
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
41944218
Self::from_be(Self::from_ne_bytes(bytes))
@@ -4221,7 +4245,7 @@ fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
42214245
}
42224246
```"),
42234247
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4224-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4248+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
42254249
#[inline]
42264250
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
42274251
Self::from_le(Self::from_ne_bytes(bytes))
@@ -4264,11 +4288,19 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
42644288
}
42654289
```"),
42664290
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4267-
#[rustc_const_unstable(feature = "const_int_conversion", issue = "53718")]
4291+
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
4292+
// SAFETY: const sound because integers are plain old datatypes so we can always
4293+
// transmute to them
4294+
#[allow_internal_unstable(const_fn_union)]
42684295
#[inline]
42694296
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
4297+
#[repr(C)]
4298+
union Bytes {
4299+
val: $SelfT,
4300+
bytes: [u8; mem::size_of::<$SelfT>()],
4301+
}
42704302
// SAFETY: integers are plain old datatypes so we can always transmute to them
4271-
unsafe { mem::transmute(bytes) }
4303+
unsafe { Bytes { bytes }.val }
42724304
}
42734305
}
42744306

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)