about summary refs log tree commit diff
path: root/src/libstd/path.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-10-04 15:14:15 +0000
committerbors <bors@rust-lang.org>2017-10-04 15:14:15 +0000
commit417ffc98dfc770c27f7f2d7430f0edf975576591 (patch)
tree4482fbdd62ff084a7cff161124abab8dc5ebcfb4 /src/libstd/path.rs
parentfd8099f0ecc2fd3980469ec45cf834ae9a941ca3 (diff)
parentf1798d3c9ab22be8f3b6f9f60f5e027be1a02085 (diff)
downloadrust-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/libstd/path.rs')
-rw-r--r--src/libstd/path.rs16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs
index 830b9dc475d..294743ed2cc 100644
--- a/src/libstd/path.rs
+++ b/src/libstd/path.rs
@@ -86,7 +86,6 @@ use fs;
 use hash::{Hash, Hasher};
 use io;
 use iter::{self, FusedIterator};
-use mem;
 use ops::{self, Deref};
 
 use ffi::{OsStr, OsString};
@@ -317,10 +316,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:
 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 {
-    mem::transmute(s)
+    &*(s as *const [u8] as *const OsStr)
 }
 
 // Detect scheme on Redox
@@ -1334,7 +1333,8 @@ impl PathBuf {
     /// [`Path`]: struct.Path.html
     #[stable(feature = "into_boxed_path", since = "1.20.0")]
     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) }
     }
 }
 
@@ -1342,7 +1342,8 @@ impl PathBuf {
 impl<'a> From<&'a Path> for Box<Path> {
     fn from(path: &'a Path) -> Box<Path> {
         let boxed: Box<OsStr> = path.inner.into();
-        unsafe { mem::transmute(boxed) }
+        let rw = Box::into_raw(boxed) as *mut Path;
+        unsafe { Box::from_raw(rw) }
     }
 }
 
@@ -1589,7 +1590,7 @@ impl Path {
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     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.
@@ -2312,7 +2313,8 @@ impl Path {
     /// [`PathBuf`]: struct.PathBuf.html
     #[stable(feature = "into_boxed_path", since = "1.20.0")]
     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;
+        let inner = unsafe { Box::from_raw(rw) };
         PathBuf { inner: OsString::from(inner) }
     }
 }