Skip to content

Commit 648a9db

Browse files
Get rid of a lot of transmutes
Most could be replaced by simple raw pointer casts (or even perfectly safe coercions!). cc #373
1 parent 4e9dd25 commit 648a9db

File tree

7 files changed

+28
-31
lines changed

7 files changed

+28
-31
lines changed

src/sched.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn sched_setaffinity(pid: Pid, cpuset: &CpuSet) -> Result<()> {
9696
let res = unsafe {
9797
libc::sched_setaffinity(pid.into(),
9898
mem::size_of::<CpuSet>() as libc::size_t,
99-
mem::transmute(cpuset))
99+
&cpuset.cpu_set)
100100
};
101101

102102
Errno::result(res).map(drop)

src/sys/quota.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,10 @@ pub fn quotactl_sync<P: ?Sized + NixPath>(which: quota::QuotaType, special: Opti
111111
}
112112

113113
pub fn quotactl_get<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, id: c_int, dqblk: &mut quota::Dqblk) -> Result<()> {
114-
use std::mem;
115-
unsafe {
116-
quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, mem::transmute(dqblk))
117-
}
114+
quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, dqblk as *mut _ as *mut c_char)
118115
}
119116

120117
pub fn quotactl_set<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, id: c_int, dqblk: &quota::Dqblk) -> Result<()> {
121-
use std::mem;
122118
let mut dqblk_copy = *dqblk;
123-
unsafe {
124-
quotactl(quota::QuotaCmd(quota::Q_SETQUOTA, which), Some(special), id, mem::transmute(&mut dqblk_copy))
125-
}
119+
quotactl(quota::QuotaCmd(quota::Q_SETQUOTA, which), Some(special), id, &mut dqblk_copy as *mut _ as *mut c_char)
126120
}

src/sys/signal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ impl SigAction {
364364
pub fn new(handler: SigHandler, flags: SaFlags, mask: SigSet) -> SigAction {
365365
let mut s = unsafe { mem::uninitialized::<libc::sigaction>() };
366366
s.sa_sigaction = match handler {
367-
SigHandler::SigDfl => unsafe { mem::transmute(libc::SIG_DFL) },
368-
SigHandler::SigIgn => unsafe { mem::transmute(libc::SIG_IGN) },
369-
SigHandler::Handler(f) => unsafe { mem::transmute(f) },
370-
SigHandler::SigAction(f) => unsafe { mem::transmute(f) },
367+
SigHandler::SigDfl => libc::SIG_DFL,
368+
SigHandler::SigIgn => libc::SIG_IGN,
369+
SigHandler::Handler(f) => f as *const extern fn(libc::c_int) as usize,
370+
SigHandler::SigAction(f) => f as *const extern fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void) as usize,
371371
};
372372
s.sa_flags = match handler {
373373
SigHandler::SigAction(_) => (flags | SA_SIGINFO).bits(),

src/sys/socket/addr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::sa_family_t;
22
use {Errno, Error, Result, NixPath};
33
use libc;
4-
use std::{fmt, hash, mem, net, ptr};
4+
use std::{fmt, hash, mem, net, ptr, slice};
55
use std::ffi::OsStr;
66
use std::path::Path;
77
use std::os::unix::ffi::OsStrExt;
@@ -581,7 +581,7 @@ impl UnixAddr {
581581
}
582582

583583
fn sun_path(&self) -> &[u8] {
584-
unsafe { mem::transmute(&self.0.sun_path[..self.1]) }
584+
unsafe { slice::from_raw_parts(self.0.sun_path.as_ptr() as *const u8, self.1) }
585585
}
586586

587587
/// If this address represents a filesystem path, return that path.

src/sys/socket/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
227227
if self.buf.len() < sizeof_cmsghdr {
228228
return None;
229229
}
230-
let cmsg: &cmsghdr = unsafe { mem::transmute(self.buf.as_ptr()) };
230+
let cmsg: &'a cmsghdr = unsafe { &*(self.buf.as_ptr() as *const cmsghdr) };
231231

232232
// This check is only in the glibc implementation of CMSG_NXTHDR
233233
// (although it claims the kernel header checks this), but such

src/sys/socket/sockopt.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ impl<T> Get<T> for GetStruct<T> {
205205
}
206206

207207
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
208-
mem::transmute(&mut self.val)
208+
&mut self.val as *mut T as *mut c_void
209209
}
210210

211211
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
212-
mem::transmute(&mut self.len)
212+
&mut self.len
213213
}
214214

215215
unsafe fn unwrap(self) -> T {
@@ -228,7 +228,7 @@ impl<'a, T> Set<'a, T> for SetStruct<'a, T> {
228228
}
229229

230230
unsafe fn ffi_ptr(&self) -> *const c_void {
231-
mem::transmute(self.ptr)
231+
self.ptr as *const T as *const c_void
232232
}
233233

234234
unsafe fn ffi_len(&self) -> socklen_t {
@@ -250,11 +250,11 @@ impl Get<bool> for GetBool {
250250
}
251251

252252
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
253-
mem::transmute(&mut self.val)
253+
&mut self.val as *mut c_int as *mut c_void
254254
}
255255

256256
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
257-
mem::transmute(&mut self.len)
257+
&mut self.len
258258
}
259259

260260
unsafe fn unwrap(self) -> bool {
@@ -273,7 +273,7 @@ impl<'a> Set<'a, bool> for SetBool {
273273
}
274274

275275
unsafe fn ffi_ptr(&self) -> *const c_void {
276-
mem::transmute(&self.val)
276+
&self.val as *const c_int as *const c_void
277277
}
278278

279279
unsafe fn ffi_len(&self) -> socklen_t {
@@ -295,11 +295,11 @@ impl Get<u8> for GetU8 {
295295
}
296296

297297
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
298-
mem::transmute(&mut self.val)
298+
&mut self.val as *mut uint8_t as *mut c_void
299299
}
300300

301301
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
302-
mem::transmute(&mut self.len)
302+
&mut self.len
303303
}
304304

305305
unsafe fn unwrap(self) -> u8 {
@@ -318,7 +318,7 @@ impl<'a> Set<'a, u8> for SetU8 {
318318
}
319319

320320
unsafe fn ffi_ptr(&self) -> *const c_void {
321-
mem::transmute(&self.val)
321+
&self.val as *const uint8_t as *const c_void
322322
}
323323

324324
unsafe fn ffi_len(&self) -> socklen_t {
@@ -340,11 +340,11 @@ impl Get<usize> for GetUsize {
340340
}
341341

342342
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
343-
mem::transmute(&mut self.val)
343+
&mut self.val as *mut c_int as *mut c_void
344344
}
345345

346346
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
347-
mem::transmute(&mut self.len)
347+
&mut self.len
348348
}
349349

350350
unsafe fn unwrap(self) -> usize {
@@ -363,7 +363,7 @@ impl<'a> Set<'a, usize> for SetUsize {
363363
}
364364

365365
unsafe fn ffi_ptr(&self) -> *const c_void {
366-
mem::transmute(&self.val)
366+
&self.val as *const c_int as *const c_void
367367
}
368368

369369
unsafe fn ffi_len(&self) -> socklen_t {

test/sys/test_socket.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use nix::sys::socket::{InetAddr, UnixAddr, getsockname};
2-
use std::mem;
2+
use std::{mem, slice};
33
use std::net::{self, Ipv6Addr, SocketAddr, SocketAddrV6};
44
use std::path::Path;
55
use std::str::FromStr;
@@ -52,10 +52,13 @@ pub fn test_inetv6_addr_to_sock_addr() {
5252

5353
#[test]
5454
pub fn test_path_to_sock_addr() {
55-
let actual = Path::new("/foo/bar");
55+
let path = "/foo/bar";
56+
let actual = Path::new(path);
5657
let addr = UnixAddr::new(actual).unwrap();
5758

58-
let expect: &'static [c_char] = unsafe { mem::transmute(&b"/foo/bar"[..]) };
59+
let expect: &[c_char] = unsafe {
60+
slice::from_raw_parts(path.as_bytes().as_ptr() as *const c_char, path.len())
61+
};
5962
assert_eq!(&addr.0.sun_path[..8], expect);
6063

6164
assert_eq!(addr.path(), Some(actual));

0 commit comments

Comments
 (0)