diff options
| author | bors <bors@rust-lang.org> | 2017-10-04 15:14:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-10-04 15:14:15 +0000 |
| commit | 417ffc98dfc770c27f7f2d7430f0edf975576591 (patch) | |
| tree | 4482fbdd62ff084a7cff161124abab8dc5ebcfb4 /src/liballoc | |
| parent | fd8099f0ecc2fd3980469ec45cf834ae9a941ca3 (diff) | |
| parent | f1798d3c9ab22be8f3b6f9f60f5e027be1a02085 (diff) | |
| download | rust-417ffc98dfc770c27f7f2d7430f0edf975576591.tar.gz rust-417ffc98dfc770c27f7f2d7430f0edf975576591.zip | |
Auto merge of #44890 - nvzqz:str-box-transmute, r=alexcrichton
Remove mem::transmute used in Box<str> conversions Given that https://github.com/rust-lang/rust/pull/44877 is failing, I decided to make a separate PR. This is done with the same motivation: to avoid `mem::transmute`-ing non `#[repr(C)]` types.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 4 | ||||
| -rw-r--r-- | src/liballoc/str.rs | 8 |
2 files changed, 4 insertions, 8 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 4341b0b2975..35c8530b4dd 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -528,9 +528,7 @@ impl<'a> From<&'a str> for Box<str> { #[stable(feature = "boxed_str_conv", since = "1.19.0")] impl From<Box<str>> for Box<[u8]> { fn from(s: Box<str>) -> Self { - unsafe { - mem::transmute(s) - } + unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) } } } diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs index 62b5f13675c..830128f2b9f 100644 --- a/src/liballoc/str.rs +++ b/src/liballoc/str.rs @@ -2047,10 +2047,8 @@ impl str { /// ``` #[stable(feature = "box_str", since = "1.4.0")] pub fn into_string(self: Box<str>) -> String { - unsafe { - let slice = mem::transmute::<Box<str>, Box<[u8]>>(self); - String::from_utf8_unchecked(slice.into_vec()) - } + let slice = Box::<[u8]>::from(self); + unsafe { String::from_utf8_unchecked(slice.into_vec()) } } /// Create a [`String`] by repeating a string `n` times. @@ -2087,5 +2085,5 @@ impl str { /// ``` #[stable(feature = "str_box_extras", since = "1.20.0")] pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> { - mem::transmute(v) + Box::from_raw(Box::into_raw(v) as *mut str) } |
