Remove mem::transmute used in Box<str> conversions

This commit is contained in:
Nikolai Vazquez
2017-09-27 14:56:20 -04:00
parent 930d3b17dd
commit 36d663fcfc
2 changed files with 4 additions and 8 deletions

View File

@@ -528,9 +528,7 @@ impl<'a> From<&'a str> for Box<str> {
#[stable(feature = "boxed_str_conv", since = "1.19.0")] #[stable(feature = "boxed_str_conv", since = "1.19.0")]
impl From<Box<str>> for Box<[u8]> { impl From<Box<str>> for Box<[u8]> {
fn from(s: Box<str>) -> Self { fn from(s: Box<str>) -> Self {
unsafe { unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
mem::transmute(s)
}
} }
} }

View File

@@ -2047,10 +2047,8 @@ impl str {
/// ``` /// ```
#[stable(feature = "box_str", since = "1.4.0")] #[stable(feature = "box_str", since = "1.4.0")]
pub fn into_string(self: Box<str>) -> String { pub fn into_string(self: Box<str>) -> String {
unsafe { let slice = Box::<[u8]>::from(self);
let slice = mem::transmute::<Box<str>, Box<[u8]>>(self); unsafe { String::from_utf8_unchecked(slice.into_vec()) }
String::from_utf8_unchecked(slice.into_vec())
}
} }
/// Create a [`String`] by repeating a string `n` times. /// Create a [`String`] by repeating a string `n` times.
@@ -2087,5 +2085,5 @@ impl str {
/// ``` /// ```
#[stable(feature = "str_box_extras", since = "1.20.0")] #[stable(feature = "str_box_extras", since = "1.20.0")]
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> { 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)
} }