Skip to content

Commit 8845dc2

Browse files
committed
Replace ffi declarations for select
1 parent 6234d4b commit 8845dc2

File tree

2 files changed

+25
-57
lines changed

2 files changed

+25
-57
lines changed

src/sys/select.rs

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,38 @@
1-
use std::ptr::null_mut;
1+
use std::mem;
22
use std::os::unix::io::RawFd;
3-
use libc::{c_int, timeval};
3+
use std::ptr::null_mut;
4+
use libc::{self, c_int};
45
use {Errno, Result};
56
use sys::time::TimeVal;
67

7-
pub const FD_SETSIZE: RawFd = 1024;
8-
9-
#[cfg(any(target_os = "macos", target_os = "ios"))]
10-
#[repr(C)]
11-
#[derive(Clone)]
12-
pub struct FdSet {
13-
bits: [i32; FD_SETSIZE as usize / 32]
14-
}
15-
16-
#[cfg(any(target_os = "macos", target_os = "ios"))]
17-
const BITS: usize = 32;
8+
pub use libc::FD_SETSIZE;
189

19-
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
10+
// FIXME: Change to repr(transparent) once it's stable
2011
#[repr(C)]
21-
#[derive(Clone)]
22-
pub struct FdSet {
23-
bits: [u64; FD_SETSIZE as usize / 64]
24-
}
25-
26-
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
27-
const BITS: usize = 64;
12+
pub struct FdSet(libc::fd_set);
2813

2914
impl FdSet {
3015
pub fn new() -> FdSet {
31-
FdSet {
32-
bits: [0; FD_SETSIZE as usize / BITS]
33-
}
16+
let mut fdset = unsafe { mem::uninitialized() };
17+
unsafe { libc::FD_ZERO(&mut fdset) };
18+
FdSet(fdset)
3419
}
3520

3621
pub fn insert(&mut self, fd: RawFd) {
37-
let fd = fd as usize;
38-
self.bits[fd / BITS] |= 1 << (fd % BITS);
22+
unsafe { libc::FD_SET(fd, &mut self.0) };
3923
}
4024

4125
pub fn remove(&mut self, fd: RawFd) {
42-
let fd = fd as usize;
43-
self.bits[fd / BITS] &= !(1 << (fd % BITS));
26+
unsafe { libc::FD_CLR(fd, &mut self.0) };
4427
}
4528

46-
pub fn contains(&self, fd: RawFd) -> bool {
47-
let fd = fd as usize;
48-
self.bits[fd / BITS] & (1 << (fd % BITS)) > 0
29+
// FIXME: Change to take `&self` once https://github.com/rust-lang/libc/pull/718 lands
30+
pub fn contains(&mut self, fd: RawFd) -> bool {
31+
unsafe { libc::FD_ISSET(fd, &mut self.0) }
4932
}
5033

5134
pub fn clear(&mut self) {
52-
for bits in &mut self.bits {
53-
*bits = 0
54-
}
55-
}
56-
}
57-
58-
mod ffi {
59-
use libc::{c_int, timeval};
60-
use super::FdSet;
61-
62-
extern {
63-
pub fn select(nfds: c_int,
64-
readfds: *mut FdSet,
65-
writefds: *mut FdSet,
66-
errorfds: *mut FdSet,
67-
timeout: *mut timeval) -> c_int;
35+
unsafe { libc::FD_ZERO(&mut self.0) };
6836
}
6937
}
7038

@@ -73,14 +41,14 @@ pub fn select(nfds: c_int,
7341
writefds: Option<&mut FdSet>,
7442
errorfds: Option<&mut FdSet>,
7543
timeout: Option<&mut TimeVal>) -> Result<c_int> {
76-
let readfds = readfds.map(|set| set as *mut FdSet).unwrap_or(null_mut());
77-
let writefds = writefds.map(|set| set as *mut FdSet).unwrap_or(null_mut());
78-
let errorfds = errorfds.map(|set| set as *mut FdSet).unwrap_or(null_mut());
79-
let timeout = timeout.map(|tv| tv as *mut TimeVal as *mut timeval)
44+
let readfds = readfds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut());
45+
let writefds = writefds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut());
46+
let errorfds = errorfds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut());
47+
let timeout = timeout.map(|tv| tv as *mut _ as *mut libc::timeval)
8048
.unwrap_or(null_mut());
8149

8250
let res = unsafe {
83-
ffi::select(nfds, readfds, writefds, errorfds, timeout)
51+
libc::select(nfds, readfds, writefds, errorfds, timeout)
8452
};
8553

8654
Errno::result(res)

test/sys/test_select.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn test_fdset() {
77
let mut fd_set = FdSet::new();
88

99
for i in 0..FD_SETSIZE {
10-
assert!(!fd_set.contains(i));
10+
assert!(!fd_set.contains(i as i32));
1111
}
1212

1313
fd_set.insert(7);
@@ -17,17 +17,17 @@ fn test_fdset() {
1717
fd_set.remove(7);
1818

1919
for i in 0..FD_SETSIZE {
20-
assert!(!fd_set.contains(i));
20+
assert!(!fd_set.contains(i as i32));
2121
}
2222

2323
fd_set.insert(1);
24-
fd_set.insert(FD_SETSIZE / 2);
25-
fd_set.insert(FD_SETSIZE - 1);
24+
fd_set.insert(FD_SETSIZE as i32 / 2);
25+
fd_set.insert(FD_SETSIZE as i32 - 1);
2626

2727
fd_set.clear();
2828

2929
for i in 0..FD_SETSIZE {
30-
assert!(!fd_set.contains(i));
30+
assert!(!fd_set.contains(i as i32));
3131
}
3232
}
3333

0 commit comments

Comments
 (0)