Skip to content

Commit def2232

Browse files
author
Ariel Ben-Yehuda
committed
Issue rust-lang#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.
1 parent 0a092a8 commit def2232

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/libnative/io/file_win32.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ impl FileDesc {
9090
pub fn handle(&self) -> libc::HANDLE {
9191
unsafe { libc::get_osfhandle(self.fd()) as libc::HANDLE }
9292
}
93+
94+
// A version of seek that takes &self so that tell can call it
95+
// - the private seek should of course take &mut self.
96+
fn seek_common(&self, pos: i64, style: io::SeekStyle) -> Result<u64, IoError> {
97+
let whence = match style {
98+
io::SeekSet => libc::FILE_BEGIN,
99+
io::SeekEnd => libc::FILE_END,
100+
io::SeekCur => libc::FILE_CURRENT,
101+
};
102+
unsafe {
103+
let mut newpos = 0;
104+
match libc::SetFilePointerEx(self.handle(), pos, &mut newpos,
105+
whence) {
106+
0 => Err(super::last_error()),
107+
_ => Ok(newpos as u64),
108+
}
109+
}
110+
}
111+
93112
}
94113

95114
impl io::Reader for FileDesc {
@@ -151,26 +170,13 @@ impl rtio::RtioFileStream for FileDesc {
151170
}
152171
Ok(())
153172
}
173+
154174
fn seek(&mut self, pos: i64, style: io::SeekStyle) -> Result<u64, IoError> {
155-
let whence = match style {
156-
io::SeekSet => libc::FILE_BEGIN,
157-
io::SeekEnd => libc::FILE_END,
158-
io::SeekCur => libc::FILE_CURRENT,
159-
};
160-
unsafe {
161-
let mut newpos = 0;
162-
match libc::SetFilePointerEx(self.handle(), pos, &mut newpos,
163-
whence) {
164-
0 => Err(super::last_error()),
165-
_ => Ok(newpos as u64),
166-
}
167-
}
175+
self.seek_common(pos, style)
168176
}
177+
169178
fn tell(&self) -> Result<u64, IoError> {
170-
// This transmute is fine because our seek implementation doesn't
171-
// actually use the mutable self at all.
172-
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
173-
unsafe { mem::transmute::<&_, &mut FileDesc>(self).seek(0, io::SeekCur) }
179+
self.seek_common(0, io::SeekCur)
174180
}
175181

176182
fn fsync(&mut self) -> Result<(), IoError> {

src/librustuv/file.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl FileWatcher {
377377
let r = FsRequest::write(&self.loop_, self.fd, buf, offset);
378378
r.map_err(uv_error_to_io_error)
379379
}
380-
fn seek_common(&mut self, pos: i64, whence: c_int) ->
380+
fn seek_common(&self, pos: i64, whence: c_int) ->
381381
Result<u64, IoError>{
382382
unsafe {
383383
match libc::lseek(self.fd, pos as libc::off_t, whence) {
@@ -446,10 +446,8 @@ impl rtio::RtioFileStream for FileWatcher {
446446
}
447447
fn tell(&self) -> Result<u64, IoError> {
448448
use libc::SEEK_CUR;
449-
// this is temporary
450-
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
451-
let self_ = unsafe { mem::transmute::<&_, &mut FileWatcher>(self) };
452-
self_.seek_common(0, SEEK_CUR)
449+
450+
self.seek_common(0, SEEK_CUR)
453451
}
454452
fn fsync(&mut self) -> Result<(), IoError> {
455453
let _m = self.fire_homing_missile();

0 commit comments

Comments
 (0)