summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2017-07-21 00:16:03 +0200
committerJonas Schievink <jonasschievink@gmail.com>2017-08-11 18:04:06 +0200
commit648a9db372c90e20090fae51563f68af205498fe (patch)
tree87256e04c78f8be7264cbefda44a72b257d216eb /src
parent4e9dd256d37e697cb775384123e37b7e2852ac1b (diff)
downloadnix-648a9db372c90e20090fae51563f68af205498fe.zip
Get rid of a lot of transmutes
Most could be replaced by simple raw pointer casts (or even perfectly safe coercions!). cc #373
Diffstat (limited to 'src')
-rw-r--r--src/sched.rs2
-rw-r--r--src/sys/quota.rs10
-rw-r--r--src/sys/signal.rs8
-rw-r--r--src/sys/socket/addr.rs4
-rw-r--r--src/sys/socket/mod.rs2
-rw-r--r--src/sys/socket/sockopt.rs24
6 files changed, 22 insertions, 28 deletions
diff --git a/src/sched.rs b/src/sched.rs
index 0b98c580..9543e50d 100644
--- a/src/sched.rs
+++ b/src/sched.rs
@@ -96,7 +96,7 @@ pub fn sched_setaffinity(pid: Pid, cpuset: &CpuSet) -> Result<()> {
let res = unsafe {
libc::sched_setaffinity(pid.into(),
mem::size_of::<CpuSet>() as libc::size_t,
- mem::transmute(cpuset))
+ &cpuset.cpu_set)
};
Errno::result(res).map(drop)
diff --git a/src/sys/quota.rs b/src/sys/quota.rs
index b66d558d..0b39fcf2 100644
--- a/src/sys/quota.rs
+++ b/src/sys/quota.rs
@@ -111,16 +111,10 @@ pub fn quotactl_sync<P: ?Sized + NixPath>(which: quota::QuotaType, special: Opti
}
pub fn quotactl_get<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, id: c_int, dqblk: &mut quota::Dqblk) -> Result<()> {
- use std::mem;
- unsafe {
- quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, mem::transmute(dqblk))
- }
+ quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, dqblk as *mut _ as *mut c_char)
}
pub fn quotactl_set<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, id: c_int, dqblk: &quota::Dqblk) -> Result<()> {
- use std::mem;
let mut dqblk_copy = *dqblk;
- unsafe {
- quotactl(quota::QuotaCmd(quota::Q_SETQUOTA, which), Some(special), id, mem::transmute(&mut dqblk_copy))
- }
+ quotactl(quota::QuotaCmd(quota::Q_SETQUOTA, which), Some(special), id, &mut dqblk_copy as *mut _ as *mut c_char)
}
diff --git a/src/sys/signal.rs b/src/sys/signal.rs
index d7e9d91d..a5ec9e3c 100644
--- a/src/sys/signal.rs
+++ b/src/sys/signal.rs
@@ -364,10 +364,10 @@ impl SigAction {
pub fn new(handler: SigHandler, flags: SaFlags, mask: SigSet) -> SigAction {
let mut s = unsafe { mem::uninitialized::<libc::sigaction>() };
s.sa_sigaction = match handler {
- SigHandler::SigDfl => unsafe { mem::transmute(libc::SIG_DFL) },
- SigHandler::SigIgn => unsafe { mem::transmute(libc::SIG_IGN) },
- SigHandler::Handler(f) => unsafe { mem::transmute(f) },
- SigHandler::SigAction(f) => unsafe { mem::transmute(f) },
+ SigHandler::SigDfl => libc::SIG_DFL,
+ SigHandler::SigIgn => libc::SIG_IGN,
+ SigHandler::Handler(f) => f as *const extern fn(libc::c_int) as usize,
+ SigHandler::SigAction(f) => f as *const extern fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void) as usize,
};
s.sa_flags = match handler {
SigHandler::SigAction(_) => (flags | SA_SIGINFO).bits(),
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs
index 71001534..d57207ed 100644
--- a/src/sys/socket/addr.rs
+++ b/src/sys/socket/addr.rs
@@ -1,7 +1,7 @@
use super::sa_family_t;
use {Errno, Error, Result, NixPath};
use libc;
-use std::{fmt, hash, mem, net, ptr};
+use std::{fmt, hash, mem, net, ptr, slice};
use std::ffi::OsStr;
use std::path::Path;
use std::os::unix::ffi::OsStrExt;
@@ -581,7 +581,7 @@ impl UnixAddr {
}
fn sun_path(&self) -> &[u8] {
- unsafe { mem::transmute(&self.0.sun_path[..self.1]) }
+ unsafe { slice::from_raw_parts(self.0.sun_path.as_ptr() as *const u8, self.1) }
}
/// If this address represents a filesystem path, return that path.
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 6ab1684a..e0f957d8 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -227,7 +227,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
if self.buf.len() < sizeof_cmsghdr {
return None;
}
- let cmsg: &cmsghdr = unsafe { mem::transmute(self.buf.as_ptr()) };
+ let cmsg: &'a cmsghdr = unsafe { &*(self.buf.as_ptr() as *const cmsghdr) };
// This check is only in the glibc implementation of CMSG_NXTHDR
// (although it claims the kernel header checks this), but such
diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs
index 3c13c539..1c6d0b85 100644
--- a/src/sys/socket/sockopt.rs
+++ b/src/sys/socket/sockopt.rs
@@ -205,11 +205,11 @@ impl<T> Get<T> for GetStruct<T> {
}
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
- mem::transmute(&mut self.val)
+ &mut self.val as *mut T as *mut c_void
}
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
- mem::transmute(&mut self.len)
+ &mut self.len
}
unsafe fn unwrap(self) -> T {
@@ -228,7 +228,7 @@ impl<'a, T> Set<'a, T> for SetStruct<'a, T> {
}
unsafe fn ffi_ptr(&self) -> *const c_void {
- mem::transmute(self.ptr)
+ self.ptr as *const T as *const c_void
}
unsafe fn ffi_len(&self) -> socklen_t {
@@ -250,11 +250,11 @@ impl Get<bool> for GetBool {
}
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
- mem::transmute(&mut self.val)
+ &mut self.val as *mut c_int as *mut c_void
}
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
- mem::transmute(&mut self.len)
+ &mut self.len
}
unsafe fn unwrap(self) -> bool {
@@ -273,7 +273,7 @@ impl<'a> Set<'a, bool> for SetBool {
}
unsafe fn ffi_ptr(&self) -> *const c_void {
- mem::transmute(&self.val)
+ &self.val as *const c_int as *const c_void
}
unsafe fn ffi_len(&self) -> socklen_t {
@@ -295,11 +295,11 @@ impl Get<u8> for GetU8 {
}
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
- mem::transmute(&mut self.val)
+ &mut self.val as *mut uint8_t as *mut c_void
}
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
- mem::transmute(&mut self.len)
+ &mut self.len
}
unsafe fn unwrap(self) -> u8 {
@@ -318,7 +318,7 @@ impl<'a> Set<'a, u8> for SetU8 {
}
unsafe fn ffi_ptr(&self) -> *const c_void {
- mem::transmute(&self.val)
+ &self.val as *const uint8_t as *const c_void
}
unsafe fn ffi_len(&self) -> socklen_t {
@@ -340,11 +340,11 @@ impl Get<usize> for GetUsize {
}
unsafe fn ffi_ptr(&mut self) -> *mut c_void {
- mem::transmute(&mut self.val)
+ &mut self.val as *mut c_int as *mut c_void
}
unsafe fn ffi_len(&mut self) -> *mut socklen_t {
- mem::transmute(&mut self.len)
+ &mut self.len
}
unsafe fn unwrap(self) -> usize {
@@ -363,7 +363,7 @@ impl<'a> Set<'a, usize> for SetUsize {
}
unsafe fn ffi_ptr(&self) -> *const c_void {
- mem::transmute(&self.val)
+ &self.val as *const c_int as *const c_void
}
unsafe fn ffi_len(&self) -> socklen_t {