Skip to content

Commit

Permalink
clean up const-hacks in int endianess conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 7, 2020
1 parent d4c940f commit a530934
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions library/core/src/num/mod.rs
Expand Up @@ -2346,17 +2346,12 @@ assert_eq!(
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
// 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)]
#[allow_internal_unstable(const_fn_transmute)]
#[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 { Bytes { val: self }.bytes }
unsafe { mem::transmute(self) }
}
}

Expand Down Expand Up @@ -2464,16 +2459,11 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
// SAFETY: const sound because integers are plain old datatypes so we can always
// transmute to them
#[allow_internal_unstable(const_fn_union)]
#[allow_internal_unstable(const_fn_transmute)]
#[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 { Bytes { bytes }.val }
unsafe { mem::transmute(bytes) }
}
}

Expand Down Expand Up @@ -4368,17 +4358,12 @@ assert_eq!(
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
// 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)]
#[allow_internal_unstable(const_fn_transmute)]
#[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 { Bytes { val: self }.bytes }
unsafe { mem::transmute(self) }
}
}

Expand Down Expand Up @@ -4486,16 +4471,11 @@ fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT),
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
// SAFETY: const sound because integers are plain old datatypes so we can always
// transmute to them
#[allow_internal_unstable(const_fn_union)]
#[allow_internal_unstable(const_fn_transmute)]
#[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 { Bytes { bytes }.val }
unsafe { mem::transmute(bytes) }
}
}

Expand Down

0 comments on commit a530934

Please sign in to comment.