Skip to content

Commit e092257

Browse files
committed
Merge #700
700: Get rid of a lot of transmutes r=asomers Most could be replaced by simple raw pointer casts (or even perfectly safe coercions!). cc #373
2 parents c48d48b + 9c9af2c commit e092257

File tree

7 files changed

+58
-61
lines changed

7 files changed

+58
-61
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: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -178,38 +178,38 @@ sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::s
178178
*
179179
*/
180180

181-
trait Get<T> {
181+
unsafe trait Get<T> {
182182
unsafe fn blank() -> Self;
183-
unsafe fn ffi_ptr(&mut self) -> *mut c_void;
184-
unsafe fn ffi_len(&mut self) -> *mut socklen_t;
183+
fn ffi_ptr(&mut self) -> *mut c_void;
184+
fn ffi_len(&mut self) -> *mut socklen_t;
185185
unsafe fn unwrap(self) -> T;
186186
}
187187

188-
trait Set<'a, T> {
188+
unsafe trait Set<'a, T> {
189189
fn new(val: &'a T) -> Self;
190-
unsafe fn ffi_ptr(&self) -> *const c_void;
191-
unsafe fn ffi_len(&self) -> socklen_t;
190+
fn ffi_ptr(&self) -> *const c_void;
191+
fn ffi_len(&self) -> socklen_t;
192192
}
193193

194194
struct GetStruct<T> {
195195
len: socklen_t,
196196
val: T,
197197
}
198198

199-
impl<T> Get<T> for GetStruct<T> {
199+
unsafe impl<T> Get<T> for GetStruct<T> {
200200
unsafe fn blank() -> Self {
201201
GetStruct {
202202
len: mem::size_of::<T>() as socklen_t,
203203
val: mem::zeroed(),
204204
}
205205
}
206206

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

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

215215
unsafe fn unwrap(self) -> T {
@@ -222,16 +222,16 @@ struct SetStruct<'a, T: 'static> {
222222
ptr: &'a T,
223223
}
224224

225-
impl<'a, T> Set<'a, T> for SetStruct<'a, T> {
225+
unsafe impl<'a, T> Set<'a, T> for SetStruct<'a, T> {
226226
fn new(ptr: &'a T) -> SetStruct<'a, T> {
227227
SetStruct { ptr: ptr }
228228
}
229229

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

234-
unsafe fn ffi_len(&self) -> socklen_t {
234+
fn ffi_len(&self) -> socklen_t {
235235
mem::size_of::<T>() as socklen_t
236236
}
237237
}
@@ -241,20 +241,20 @@ struct GetBool {
241241
val: c_int,
242242
}
243243

244-
impl Get<bool> for GetBool {
244+
unsafe impl Get<bool> for GetBool {
245245
unsafe fn blank() -> Self {
246246
GetBool {
247247
len: mem::size_of::<c_int>() as socklen_t,
248248
val: mem::zeroed(),
249249
}
250250
}
251251

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

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

260260
unsafe fn unwrap(self) -> bool {
@@ -267,16 +267,16 @@ struct SetBool {
267267
val: c_int,
268268
}
269269

270-
impl<'a> Set<'a, bool> for SetBool {
270+
unsafe impl<'a> Set<'a, bool> for SetBool {
271271
fn new(val: &'a bool) -> SetBool {
272272
SetBool { val: if *val { 1 } else { 0 } }
273273
}
274274

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

279-
unsafe fn ffi_len(&self) -> socklen_t {
279+
fn ffi_len(&self) -> socklen_t {
280280
mem::size_of::<c_int>() as socklen_t
281281
}
282282
}
@@ -286,20 +286,20 @@ struct GetU8 {
286286
val: uint8_t,
287287
}
288288

289-
impl Get<u8> for GetU8 {
289+
unsafe impl Get<u8> for GetU8 {
290290
unsafe fn blank() -> Self {
291291
GetU8 {
292292
len: mem::size_of::<uint8_t>() as socklen_t,
293293
val: mem::zeroed(),
294294
}
295295
}
296296

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

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

305305
unsafe fn unwrap(self) -> u8 {
@@ -312,16 +312,16 @@ struct SetU8 {
312312
val: uint8_t,
313313
}
314314

315-
impl<'a> Set<'a, u8> for SetU8 {
315+
unsafe impl<'a> Set<'a, u8> for SetU8 {
316316
fn new(val: &'a u8) -> SetU8 {
317317
SetU8 { val: *val as uint8_t }
318318
}
319319

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

324-
unsafe fn ffi_len(&self) -> socklen_t {
324+
fn ffi_len(&self) -> socklen_t {
325325
mem::size_of::<c_int>() as socklen_t
326326
}
327327
}
@@ -331,20 +331,20 @@ struct GetUsize {
331331
val: c_int,
332332
}
333333

334-
impl Get<usize> for GetUsize {
334+
unsafe impl Get<usize> for GetUsize {
335335
unsafe fn blank() -> Self {
336336
GetUsize {
337337
len: mem::size_of::<c_int>() as socklen_t,
338338
val: mem::zeroed(),
339339
}
340340
}
341341

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

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

350350
unsafe fn unwrap(self) -> usize {
@@ -357,16 +357,16 @@ struct SetUsize {
357357
val: c_int,
358358
}
359359

360-
impl<'a> Set<'a, usize> for SetUsize {
360+
unsafe impl<'a> Set<'a, usize> for SetUsize {
361361
fn new(val: &'a usize) -> SetUsize {
362362
SetUsize { val: *val as c_int }
363363
}
364364

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

369-
unsafe fn ffi_len(&self) -> socklen_t {
369+
fn ffi_len(&self) -> socklen_t {
370370
mem::size_of::<c_int>() as socklen_t
371371
}
372372
}

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)