diff options
| author | Trevor Spiteri <tspiteri@ieee.org> | 2020-02-26 11:59:37 +0100 |
|---|---|---|
| committer | Trevor Spiteri <tspiteri@ieee.org> | 2020-02-26 11:59:37 +0100 |
| commit | 87f0dc63a864c6d9a3c34aa4052762dbcd316c91 (patch) | |
| tree | 66fb86724e9f14137cd649faf81793a09de98136 /src | |
| parent | d15a98b87815850ef9dd11564e896b6f19b724c3 (diff) | |
use unions instead of transmute and add const safety comments
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/num/mod.rs | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 22897680567..b64abc31936 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2251,12 +2251,19 @@ assert_eq!( ```"), #[stable(feature = "int_to_from_bytes", since = "1.32.0")] #[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] - #[allow_internal_unstable(const_transmute)] + // SAFETY: const sound because integers are plain old datatypes so we can always + // transmute them to arrays of bytes + #[allow_internal_unstable(const_fn_union)] #[inline] pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] { + #[repr(C)] + union Bytes { + val: $SelfT, + bytes: [u8; mem::size_of::<$SelfT>()], + } // SAFETY: integers are plain old datatypes so we can always transmute them to // arrays of bytes - unsafe { mem::transmute(self) } + unsafe { Bytes { val: self }.bytes } } } @@ -2362,11 +2369,18 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), ```"), #[stable(feature = "int_to_from_bytes", since = "1.32.0")] #[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] - #[allow_internal_unstable(const_transmute)] + // 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 } } } } @@ -4190,12 +4204,19 @@ assert_eq!( ```"), #[stable(feature = "int_to_from_bytes", since = "1.32.0")] #[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] - #[allow_internal_unstable(const_transmute)] + // SAFETY: const sound because integers are plain old datatypes so we can always + // transmute them to arrays of bytes + #[allow_internal_unstable(const_fn_union)] #[inline] pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] { + #[repr(C)] + union Bytes { + val: $SelfT, + bytes: [u8; mem::size_of::<$SelfT>()], + } // SAFETY: integers are plain old datatypes so we can always transmute them to // arrays of bytes - unsafe { mem::transmute(self) } + unsafe { Bytes { val: self }.bytes } } } @@ -4301,11 +4322,18 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), ```"), #[stable(feature = "int_to_from_bytes", since = "1.32.0")] #[rustc_const_stable(feature = "const_int_conversion", since = "1.43.0")] - #[allow_internal_unstable(const_transmute)] + // 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 } } } } |
