Remove mem::transmute used in OsStr conversions

This commit is contained in:
Nikolai Vazquez 2017-09-28 08:07:49 -04:00
parent 755fd29b60
commit 33eaa94f90

View file

@ -260,7 +260,8 @@ impl OsString {
/// ```
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
pub fn into_boxed_os_str(self) -> Box<OsStr> {
unsafe { mem::transmute(self.inner.into_box()) }
let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
@ -394,7 +395,7 @@ impl OsStr {
}
fn from_inner(inner: &Slice) -> &OsStr {
unsafe { mem::transmute(inner) }
unsafe { &*(inner as *const Slice as *const OsStr) }
}
/// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
@ -511,8 +512,8 @@ impl OsStr {
/// [`OsString`]: struct.OsString.html
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
pub fn into_os_string(self: Box<OsStr>) -> OsString {
let inner: Box<Slice> = unsafe { mem::transmute(self) };
OsString { inner: Buf::from_box(inner) }
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
OsString { inner: Buf::from_box(boxed) }
}
/// Gets the underlying byte representation.
@ -520,14 +521,15 @@ impl OsStr {
/// Note: it is *crucial* that this API is private, to avoid
/// revealing the internal, platform-specific encodings.
fn bytes(&self) -> &[u8] {
unsafe { mem::transmute(&self.inner) }
&self.inner.inner
}
}
#[stable(feature = "box_from_os_str", since = "1.17.0")]
impl<'a> From<&'a OsStr> for Box<OsStr> {
fn from(s: &'a OsStr) -> Box<OsStr> {
unsafe { mem::transmute(s.inner.into_box()) }
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}
@ -548,7 +550,8 @@ impl From<OsString> for Box<OsStr> {
#[stable(feature = "box_default_extra", since = "1.17.0")]
impl Default for Box<OsStr> {
fn default() -> Box<OsStr> {
unsafe { mem::transmute(Slice::empty_box()) }
let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
unsafe { Box::from_raw(rw) }
}
}