From def2232595a68897cbaa1d3dc0820da5fe56f429 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 28 May 2014 13:47:30 +0300 Subject: [PATCH 1/2] Issue #13933: Remove transmute_mut from IO The IO libraries casted self to mut so they can pass it to seek(SEEK_CUR, 0). Fix this by introducing a private seek function that takes &self - of course one should be careful with it if he lacks an exclusive reference to self. --- src/libnative/io/file_win32.rs | 40 +++++++++++++++++++--------------- src/librustuv/file.rs | 8 +++---- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/libnative/io/file_win32.rs b/src/libnative/io/file_win32.rs index c9a48db69207a..4f1f3b3ca26f7 100644 --- a/src/libnative/io/file_win32.rs +++ b/src/libnative/io/file_win32.rs @@ -90,6 +90,25 @@ impl FileDesc { pub fn handle(&self) -> libc::HANDLE { unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE } } + + // A version of seek that takes &self so that tell can call it + // - the private seek should of course take &mut self. + fn seek_common(&self, pos: i64, style: io::SeekStyle) -> Result { + let whence = match style { + io::SeekSet => libc::FILE_BEGIN, + io::SeekEnd => libc::FILE_END, + io::SeekCur => libc::FILE_CURRENT, + }; + unsafe { + let mut newpos = 0; + match libc::SetFilePointerEx(self.handle(), pos, &mut newpos, + whence) { + 0 => Err(super::last_error()), + _ => Ok(newpos as u64), + } + } + } + } impl io::Reader for FileDesc { @@ -151,26 +170,13 @@ impl rtio::RtioFileStream for FileDesc { } Ok(()) } + fn seek(&mut self, pos: i64, style: io::SeekStyle) -> Result { - let whence = match style { - io::SeekSet => libc::FILE_BEGIN, - io::SeekEnd => libc::FILE_END, - io::SeekCur => libc::FILE_CURRENT, - }; - unsafe { - let mut newpos = 0; - match libc::SetFilePointerEx(self.handle(), pos, &mut newpos, - whence) { - 0 => Err(super::last_error()), - _ => Ok(newpos as u64), - } - } + self.seek_common(pos, style) } + fn tell(&self) -> Result { - // This transmute is fine because our seek implementation doesn't - // actually use the mutable self at all. - // FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes - unsafe { mem::transmute::<&_, &mut FileDesc>(self).seek(0, io::SeekCur) } + self.seek_common(0, io::SeekCur) } fn fsync(&mut self) -> Result<(), IoError> { diff --git a/src/librustuv/file.rs b/src/librustuv/file.rs index 12636a3c490ad..7143f420b08a1 100644 --- a/src/librustuv/file.rs +++ b/src/librustuv/file.rs @@ -377,7 +377,7 @@ impl FileWatcher { let r = FsRequest::write(&self.loop_, self.fd, buf, offset); r.map_err(uv_error_to_io_error) } - fn seek_common(&mut self, pos: i64, whence: c_int) -> + fn seek_common(&self, pos: i64, whence: c_int) -> Result{ unsafe { match libc::lseek(self.fd, pos as libc::off_t, whence) { @@ -446,10 +446,8 @@ impl rtio::RtioFileStream for FileWatcher { } fn tell(&self) -> Result { use libc::SEEK_CUR; - // this is temporary - // FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes - let self_ = unsafe { mem::transmute::<&_, &mut FileWatcher>(self) }; - self_.seek_common(0, SEEK_CUR) + + self.seek_common(0, SEEK_CUR) } fn fsync(&mut self) -> Result<(), IoError> { let _m = self.fire_homing_missile(); From 2e8bc9924c096cb8f889d3f46ecce7ebe622b964 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 28 May 2014 14:09:50 +0300 Subject: [PATCH 2/2] Issue #13933: Remove transmute_mut from Arc directly use the internal pointer instead. --- src/liballoc/arc.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index a408bf8e284e7..546e4e5269979 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -160,7 +160,8 @@ impl Arc { // reference count is guaranteed to be 1 at this point, and we required // the Arc itself to be `mut`, so we're returning the only possible // reference to the inner data. - unsafe { mem::transmute::<&_, &mut _>(self.deref()) } + let inner = unsafe { &mut *self._ptr }; + &mut inner.data } }