about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorTobias Bucher <tobiasbucher5991@gmail.com>2015-07-24 03:04:55 +0200
committerTobias Bucher <tobiasbucher5991@gmail.com>2015-08-09 22:05:22 +0200
commit22ec5f4af7b5a85ad375d672ed727571b49f3cad (patch)
treeeea29f1286398aaaa9d55f23163ddcc49b033eeb /src/libstd
parentfebdc3b201bcce1546c88e3be1b956d3f90d3059 (diff)
downloadrust-22ec5f4af7b5a85ad375d672ed727571b49f3cad.tar.gz
rust-22ec5f4af7b5a85ad375d672ed727571b49f3cad.zip
Replace many uses of `mem::transmute` with more specific functions
The replacements are functions that usually use a single `mem::transmute` in
their body and restrict input and output via more concrete types than `T` and
`U`. Worth noting are the `transmute` functions for slices and the `from_utf8*`
family for mutable slices. Additionally, `mem::transmute` was often used for
casting raw pointers, when you can already cast raw pointers just fine with
`as`.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/ascii.rs5
-rw-r--r--src/libstd/error.rs4
-rw-r--r--src/libstd/ffi/c_str.rs2
-rw-r--r--src/libstd/ffi/os_str.rs10
-rw-r--r--src/libstd/lib.rs2
-rw-r--r--src/libstd/path.rs8
-rw-r--r--src/libstd/rand/reader.rs4
-rw-r--r--src/libstd/sync/mpsc/select.rs5
-rw-r--r--src/libstd/sys/unix/backtrace.rs3
9 files changed, 23 insertions, 20 deletions
diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs
index cd9dadd1be9..f003948be7b 100644
--- a/src/libstd/ascii.rs
+++ b/src/libstd/ascii.rs
@@ -15,7 +15,6 @@
 use prelude::v1::*;
 
 use ops::Range;
-use mem;
 
 /// Extension methods for ASCII-subset only operations on owned strings
 #[unstable(feature = "owned_ascii_ext",
@@ -186,12 +185,12 @@ impl AsciiExt for str {
     }
 
     fn make_ascii_uppercase(&mut self) {
-        let me: &mut [u8] = unsafe { mem::transmute(self) };
+        let me: &mut [u8] = unsafe { self.as_bytes_mut() };
         me.make_ascii_uppercase()
     }
 
     fn make_ascii_lowercase(&mut self) {
-        let me: &mut [u8] = unsafe { mem::transmute(self) };
+        let me: &mut [u8] = unsafe { self.as_bytes_mut() };
         me.make_ascii_lowercase()
     }
 }
diff --git a/src/libstd/error.rs b/src/libstd/error.rs
index 4d08f08bb6e..f0f481d3721 100644
--- a/src/libstd/error.rs
+++ b/src/libstd/error.rs
@@ -192,7 +192,7 @@ impl Error + 'static {
                 let to: TraitObject = transmute(self);
 
                 // Extract the data pointer
-                Some(transmute(to.data))
+                Some(&*(to.data as *const T))
             }
         } else {
             None
@@ -210,7 +210,7 @@ impl Error + 'static {
                 let to: TraitObject = transmute(self);
 
                 // Extract the data pointer
-                Some(transmute(to.data))
+                Some(&mut *(to.data as *const T as *mut T))
             }
         } else {
             None
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs
index 6eb0719d9f6..adbda6ae242 100644
--- a/src/libstd/ffi/c_str.rs
+++ b/src/libstd/ffi/c_str.rs
@@ -395,7 +395,7 @@ impl CStr {
     /// > length calculation whenever this method is called.
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn to_bytes_with_nul(&self) -> &[u8] {
-        unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.inner) }
+        unsafe { slice::transmute(&self.inner) }
     }
 
     /// Yields a `&str` slice if the `CStr` contains valid UTF-8.
diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs
index 47b8230e430..83d76481d49 100644
--- a/src/libstd/ffi/os_str.rs
+++ b/src/libstd/ffi/os_str.rs
@@ -134,7 +134,7 @@ impl ops::Index<ops::RangeFull> for OsString {
 
     #[inline]
     fn index(&self, _index: ops::RangeFull) -> &OsStr {
-        unsafe { mem::transmute(self.inner.as_slice()) }
+        OsStr::from_inner(self.inner.as_slice())
     }
 }
 
@@ -226,6 +226,10 @@ impl OsStr {
         s.as_ref()
     }
 
+    fn from_inner(inner: &Slice) -> &OsStr {
+        unsafe { mem::transmute(inner) }
+    }
+
     /// Yields a `&str` slice if the `OsStr` is valid unicode.
     ///
     /// This conversion may entail doing a check for UTF-8 validity.
@@ -387,14 +391,14 @@ impl AsRef<OsStr> for OsString {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl AsRef<OsStr> for str {
     fn as_ref(&self) -> &OsStr {
-        unsafe { mem::transmute(Slice::from_str(self)) }
+        OsStr::from_inner(Slice::from_str(self))
     }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl AsRef<OsStr> for String {
     fn as_ref(&self) -> &OsStr {
-        unsafe { mem::transmute(Slice::from_str(self)) }
+        (&**self).as_ref()
     }
 }
 
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index 7baa7558e52..65f195be4e8 100644
--- a/src/libstd/lib.rs
+++ b/src/libstd/lib.rs
@@ -234,7 +234,9 @@
 #![feature(reflect_marker)]
 #![feature(slice_bytes)]
 #![feature(slice_patterns)]
+#![feature(slice_transmute)]
 #![feature(staged_api)]
+#![feature(str_as_bytes_mut)]
 #![feature(str_char)]
 #![feature(str_internals)]
 #![feature(unboxed_closures)]
diff --git a/src/libstd/path.rs b/src/libstd/path.rs
index 4a4db61c3b9..c3a887cbcb8 100644
--- a/src/libstd/path.rs
+++ b/src/libstd/path.rs
@@ -942,7 +942,7 @@ pub struct PathBuf {
 
 impl PathBuf {
     fn as_mut_vec(&mut self) -> &mut Vec<u8> {
-        unsafe { mem::transmute(self) }
+        unsafe { &mut *(self as *mut PathBuf as *mut Vec<u8>) }
     }
 
     /// Allocates an empty `PathBuf`.
@@ -1126,7 +1126,7 @@ impl ops::Deref for PathBuf {
     type Target = Path;
 
     fn deref(&self) -> &Path {
-        unsafe { mem::transmute(&self.inner[..]) }
+        Path::new(&self.inner)
     }
 }
 
@@ -1227,11 +1227,11 @@ impl Path {
     // The following (private!) function allows construction of a path from a u8
     // slice, which is only safe when it is known to follow the OsStr encoding.
     unsafe fn from_u8_slice(s: &[u8]) -> &Path {
-        mem::transmute(s)
+        Path::new(u8_slice_as_os_str(s))
     }
     // The following (private!) function reveals the byte encoding used for OsStr.
     fn as_u8_slice(&self) -> &[u8] {
-        unsafe { mem::transmute(self) }
+        os_str_as_u8_slice(&self.inner)
     }
 
     /// Directly wrap a string slice as a `Path` slice.
diff --git a/src/libstd/rand/reader.rs b/src/libstd/rand/reader.rs
index 665f423c3f1..8ac2722f65a 100644
--- a/src/libstd/rand/reader.rs
+++ b/src/libstd/rand/reader.rs
@@ -73,8 +73,8 @@ mod tests {
     fn test_reader_rng_u64() {
         // transmute from the target to avoid endianness concerns.
         let v = &[0, 0, 0, 0, 0, 0, 0, 1,
-                  0  , 0, 0, 0, 0, 0, 0, 2,
-                  0,   0, 0, 0, 0, 0, 0, 3][..];
+                  0, 0, 0, 0, 0, 0, 0, 2,
+                  0, 0, 0, 0, 0, 0, 0, 3][..];
         let mut rng = ReaderRng::new(v);
 
         assert_eq!(rng.next_u64(), 1u64.to_be());
diff --git a/src/libstd/sync/mpsc/select.rs b/src/libstd/sync/mpsc/select.rs
index 1d31ac165f6..c46e61cf414 100644
--- a/src/libstd/sync/mpsc/select.rs
+++ b/src/libstd/sync/mpsc/select.rs
@@ -62,7 +62,6 @@ use core::prelude::v1::*;
 
 use core::cell::{Cell, UnsafeCell};
 use core::marker;
-use core::mem;
 use core::ptr;
 use core::usize;
 
@@ -281,7 +280,7 @@ impl<'rx, T: Send> Handle<'rx, T> {
     pub unsafe fn add(&mut self) {
         if self.added { return }
         let selector = &mut *self.selector;
-        let me: *mut Handle<'static, ()> = mem::transmute(&*self);
+        let me = self as *mut Handle<'rx, T> as *mut Handle<'static, ()>;
 
         if selector.head.is_null() {
             selector.head = me;
@@ -302,7 +301,7 @@ impl<'rx, T: Send> Handle<'rx, T> {
         if !self.added { return }
 
         let selector = &mut *self.selector;
-        let me: *mut Handle<'static, ()> = mem::transmute(&*self);
+        let me = self as *mut Handle<'rx, T> as *mut Handle<'static, ()>;
 
         if self.prev.is_null() {
             assert_eq!(selector.head, me);
diff --git a/src/libstd/sys/unix/backtrace.rs b/src/libstd/sys/unix/backtrace.rs
index ae8bfb07aaf..4128431ee64 100644
--- a/src/libstd/sys/unix/backtrace.rs
+++ b/src/libstd/sys/unix/backtrace.rs
@@ -90,7 +90,6 @@ use io::prelude::*;
 use ffi::CStr;
 use io;
 use libc;
-use mem;
 use str;
 use sync::StaticMutex;
 
@@ -168,7 +167,7 @@ pub fn write(w: &mut Write) -> io::Result<()> {
 
     extern fn trace_fn(ctx: *mut uw::_Unwind_Context,
                        arg: *mut libc::c_void) -> uw::_Unwind_Reason_Code {
-        let cx: &mut Context = unsafe { mem::transmute(arg) };
+        let cx: &mut Context = unsafe { &mut *(arg as *mut Context) };
         let mut ip_before_insn = 0;
         let mut ip = unsafe {
             uw::_Unwind_GetIPInfo(ctx, &mut ip_before_insn) as *mut libc::c_void