diff options
| author | bors <bors@rust-lang.org> | 2015-08-10 18:46:21 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-08-10 18:46:21 +0000 |
| commit | 3d69bec88119e0471a98cd2075fcf53c43ebca3e (patch) | |
| tree | b84fecb519b6883f4d75d521a90f9d476782e4cb /src/libstd | |
| parent | 96a1f40402cdd05d29d1f1c0b62001254db8dcf6 (diff) | |
| parent | 33af24ca4c2a6edaed0509a5b9ac3fde964a2847 (diff) | |
| download | rust-3d69bec88119e0471a98cd2075fcf53c43ebca3e.tar.gz rust-3d69bec88119e0471a98cd2075fcf53c43ebca3e.zip | |
Auto merge of #27252 - tbu-:pr_less_transmutes, r=alexcrichton
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`. This builds upon #27233.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ascii.rs | 2 | ||||
| -rw-r--r-- | src/libstd/error.rs | 4 | ||||
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/ffi/os_str.rs | 10 | ||||
| -rw-r--r-- | src/libstd/path.rs | 8 | ||||
| -rw-r--r-- | src/libstd/rand/reader.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sync/mpsc/select.rs | 5 | ||||
| -rw-r--r-- | src/libstd/sys/unix/backtrace.rs | 3 |
8 files changed, 20 insertions, 18 deletions
diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index cd9dadd1be9..ded572e82ff 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -14,8 +14,8 @@ use prelude::v1::*; -use ops::Range; use mem; +use ops::Range; /// Extension methods for ASCII-subset only operations on owned strings #[unstable(feature = "owned_ascii_ext", 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..3e503074ab4 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 { mem::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/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 |
