Skip to content

Commit 87f0dc6

Browse files
committed
use unions instead of transmute and add const safety comments
1 parent d15a98b commit 87f0dc6

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/libcore/num/mod.rs

+36-8
Original file line numberDiff line numberDiff line change
@@ -2251,12 +2251,19 @@ assert_eq!(
22512251
```"),
22522252
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
22532253
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
2254-
#[allow_internal_unstable(const_transmute)]
2254+
// SAFETY: const sound because integers are plain old datatypes so we can always
2255+
// transmute them to arrays of bytes
2256+
#[allow_internal_unstable(const_fn_union)]
22552257
#[inline]
22562258
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
2259+
#[repr(C)]
2260+
union Bytes {
2261+
val: $SelfT,
2262+
bytes: [u8; mem::size_of::<$SelfT>()],
2263+
}
22572264
// SAFETY: integers are plain old datatypes so we can always transmute them to
22582265
// arrays of bytes
2259-
unsafe { mem::transmute(self) }
2266+
unsafe { Bytes { val: self }.bytes }
22602267
}
22612268
}
22622269

@@ -2362,11 +2369,18 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
23622369
```"),
23632370
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
23642371
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
2365-
#[allow_internal_unstable(const_transmute)]
2372+
// SAFETY: const sound because integers are plain old datatypes so we can always
2373+
// transmute to them
2374+
#[allow_internal_unstable(const_fn_union)]
23662375
#[inline]
23672376
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2377+
#[repr(C)]
2378+
union Bytes {
2379+
val: $SelfT,
2380+
bytes: [u8; mem::size_of::<$SelfT>()],
2381+
}
23682382
// SAFETY: integers are plain old datatypes so we can always transmute to them
2369-
unsafe { mem::transmute(bytes) }
2383+
unsafe { Bytes { bytes }.val }
23702384
}
23712385
}
23722386
}
@@ -4190,12 +4204,19 @@ assert_eq!(
41904204
```"),
41914205
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
41924206
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
4193-
#[allow_internal_unstable(const_transmute)]
4207+
// SAFETY: const sound because integers are plain old datatypes so we can always
4208+
// transmute them to arrays of bytes
4209+
#[allow_internal_unstable(const_fn_union)]
41944210
#[inline]
41954211
pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
4212+
#[repr(C)]
4213+
union Bytes {
4214+
val: $SelfT,
4215+
bytes: [u8; mem::size_of::<$SelfT>()],
4216+
}
41964217
// SAFETY: integers are plain old datatypes so we can always transmute them to
41974218
// arrays of bytes
4198-
unsafe { mem::transmute(self) }
4219+
unsafe { Bytes { val: self }.bytes }
41994220
}
42004221
}
42014222

@@ -4301,11 +4322,18 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
43014322
```"),
43024323
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
43034324
#[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")]
4304-
#[allow_internal_unstable(const_transmute)]
4325+
// SAFETY: const sound because integers are plain old datatypes so we can always
4326+
// transmute to them
4327+
#[allow_internal_unstable(const_fn_union)]
43054328
#[inline]
43064329
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
4330+
#[repr(C)]
4331+
union Bytes {
4332+
val: $SelfT,
4333+
bytes: [u8; mem::size_of::<$SelfT>()],
4334+
}
43074335
// SAFETY: integers are plain old datatypes so we can always transmute to them
4308-
unsafe { mem::transmute(bytes) }
4336+
unsafe { Bytes { bytes }.val }
43094337
}
43104338
}
43114339
}

0 commit comments

Comments
 (0)