Skip to content

Commit 1767c8b

Browse files
Replaced union with transmute
1 parent a915bbf commit 1767c8b

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

library/core/src/str/mod.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,6 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
381381
Ok(unsafe { from_utf8_unchecked_mut(v) })
382382
}
383383

384-
#[repr(C)]
385-
union StrOrSlice<'a> {
386-
str: &'a str,
387-
slice: &'a [u8],
388-
}
389-
390384
/// Converts a slice of bytes to a string slice without checking
391385
/// that the string contains valid UTF-8.
392386
///
@@ -422,11 +416,11 @@ union StrOrSlice<'a> {
422416
#[stable(feature = "rust1", since = "1.0.0")]
423417
#[rustc_const_unstable(feature = "const_str_from_utf8_unchecked", issue = "75196")]
424418
#[allow(unused_attributes)]
425-
#[allow_internal_unstable(const_fn_union)]
419+
#[allow_internal_unstable(const_fn_transmute)]
426420
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
427421
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8.
428422
// Also relies on `&str` and `&[u8]` having the same layout.
429-
unsafe { StrOrSlice { slice: v }.str }
423+
unsafe { mem::transmute(self) }
430424
}
431425

432426
/// Converts a slice of bytes to a string slice without checking
@@ -2355,10 +2349,10 @@ impl str {
23552349
#[rustc_const_stable(feature = "str_as_bytes", since = "1.32.0")]
23562350
#[inline(always)]
23572351
#[allow(unused_attributes)]
2358-
#[allow_internal_unstable(const_fn_union)]
2352+
#[allow_internal_unstable(const_fn_transmute)]
23592353
pub const fn as_bytes(&self) -> &[u8] {
23602354
// SAFETY: const sound because we transmute two types with the same layout
2361-
unsafe { StrOrSlice { str: self }.slice }
2355+
unsafe { mem::transmute(self) }
23622356
}
23632357

23642358
/// Converts a mutable string slice to a mutable byte slice.

0 commit comments

Comments
 (0)