1
Fork 0

Remove mem::transmute used in Path conversions

This commit is contained in:
Nikolai Vazquez 2017-09-28 10:29:58 -04:00
parent 627998e86f
commit 48c20b6a50

View file

@ -317,10 +317,10 @@ fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I>
// See note at the top of this module to understand why these are used: // See note at the top of this module to understand why these are used:
fn os_str_as_u8_slice(s: &OsStr) -> &[u8] { fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
unsafe { mem::transmute(s) } unsafe { &*(s as *const OsStr as *const [u8]) }
} }
unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr { unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
mem::transmute(s) &*(s as *const [u8] as *const OsStr)
} }
// Detect scheme on Redox // Detect scheme on Redox
@ -1334,15 +1334,16 @@ impl PathBuf {
/// [`Path`]: struct.Path.html /// [`Path`]: struct.Path.html
#[stable(feature = "into_boxed_path", since = "1.20.0")] #[stable(feature = "into_boxed_path", since = "1.20.0")]
pub fn into_boxed_path(self) -> Box<Path> { pub fn into_boxed_path(self) -> Box<Path> {
unsafe { mem::transmute(self.inner.into_boxed_os_str()) } let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
unsafe { Box::from_raw(rw) }
} }
} }
#[stable(feature = "box_from_path", since = "1.17.0")] #[stable(feature = "box_from_path", since = "1.17.0")]
impl<'a> From<&'a Path> for Box<Path> { impl<'a> From<&'a Path> for Box<Path> {
fn from(path: &'a Path) -> Box<Path> { fn from(path: &'a Path) -> Box<Path> {
let boxed: Box<OsStr> = path.inner.into(); let rw = Box::into_raw(Box::from(&path.inner)) as *mut Path;
unsafe { mem::transmute(boxed) } unsafe { Box::from_raw(rw) }
} }
} }
@ -1589,7 +1590,7 @@ impl Path {
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path { pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
unsafe { mem::transmute(s.as_ref()) } unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
} }
/// Yields the underlying [`OsStr`] slice. /// Yields the underlying [`OsStr`] slice.
@ -2312,8 +2313,8 @@ impl Path {
/// [`PathBuf`]: struct.PathBuf.html /// [`PathBuf`]: struct.PathBuf.html
#[stable(feature = "into_boxed_path", since = "1.20.0")] #[stable(feature = "into_boxed_path", since = "1.20.0")]
pub fn into_path_buf(self: Box<Path>) -> PathBuf { pub fn into_path_buf(self: Box<Path>) -> PathBuf {
let inner: Box<OsStr> = unsafe { mem::transmute(self) }; let rw = Box::into_raw(self) as *mut OsStr;
PathBuf { inner: OsString::from(inner) } unsafe { Box::from_raw(rw) }
} }
} }