|
1 |
| -use libc::c_int; |
| 1 | +use libc; |
2 | 2 | use {Errno, Result};
|
3 | 3 |
|
4 |
| -pub use self::ffi::PollFd; |
5 |
| -pub use self::ffi::consts::*; |
6 |
| - |
7 |
| -mod ffi { |
8 |
| - use libc::c_int; |
9 |
| - pub use self::consts::*; |
10 |
| - |
11 |
| - #[derive(Clone, Copy, Debug)] |
12 |
| - #[repr(C)] |
13 |
| - pub struct PollFd { |
14 |
| - pub fd: c_int, |
15 |
| - pub events: EventFlags, |
16 |
| - pub revents: EventFlags |
17 |
| - } |
18 |
| - |
19 |
| - #[cfg(target_os = "linux")] |
20 |
| - pub mod consts { |
21 |
| - use libc::{c_short, c_ulong}; |
| 4 | +#[repr(C)] |
| 5 | +#[derive(Clone, Copy)] |
| 6 | +pub struct PollFd { |
| 7 | + pollfd: libc::pollfd, |
| 8 | +} |
22 | 9 |
|
23 |
| - bitflags! { |
24 |
| - flags EventFlags: c_short { |
25 |
| - const POLLIN = 0x001, |
26 |
| - const POLLPRI = 0x002, |
27 |
| - const POLLOUT = 0x004, |
28 |
| - const POLLRDNORM = 0x040, |
29 |
| - const POLLWRNORM = 0x100, |
30 |
| - const POLLRDBAND = 0x080, |
31 |
| - const POLLWRBAND = 0x200, |
32 |
| - const POLLERR = 0x008, |
33 |
| - const POLLHUP = 0x010, |
34 |
| - const POLLNVAL = 0x020, |
35 |
| - } |
| 10 | +impl PollFd { |
| 11 | + pub fn new(fd: libc::c_int, events: EventFlags, revents: EventFlags) -> PollFd { |
| 12 | + PollFd { |
| 13 | + pollfd: libc::pollfd { |
| 14 | + fd: fd, |
| 15 | + events: events.bits(), |
| 16 | + revents: revents.bits(), |
| 17 | + }, |
36 | 18 | }
|
37 |
| - |
38 |
| - pub type nfds_t = c_ulong; |
39 | 19 | }
|
40 | 20 |
|
41 |
| - #[cfg(target_os = "macos")] |
42 |
| - pub mod consts { |
43 |
| - use libc::{c_short, c_uint}; |
44 |
| - |
45 |
| - bitflags! { |
46 |
| - flags EventFlags: c_short { |
47 |
| - const POLLIN = 0x0001, |
48 |
| - const POLLPRI = 0x0002, |
49 |
| - const POLLOUT = 0x0004, |
50 |
| - const POLLRDNORM = 0x0040, |
51 |
| - const POLLWRNORM = 0x0004, |
52 |
| - const POLLRDBAND = 0x0080, |
53 |
| - const POLLWRBAND = 0x0100, |
54 |
| - const POLLERR = 0x0008, |
55 |
| - const POLLHUP = 0x0010, |
56 |
| - const POLLNVAL = 0x0020, |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - pub type nfds_t = c_uint; |
| 21 | + pub fn revents(&self) -> Option<EventFlags> { |
| 22 | + EventFlags::from_bits(self.pollfd.revents) |
61 | 23 | }
|
| 24 | +} |
62 | 25 |
|
63 |
| - #[allow(improper_ctypes)] |
64 |
| - extern { |
65 |
| - pub fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int; |
| 26 | +libc_bitflags! { |
| 27 | + flags EventFlags: libc::c_short { |
| 28 | + POLLIN, |
| 29 | + POLLPRI, |
| 30 | + POLLOUT, |
| 31 | + POLLRDNORM, |
| 32 | + POLLWRNORM, |
| 33 | + POLLRDBAND, |
| 34 | + POLLWRBAND, |
| 35 | + POLLERR, |
| 36 | + POLLHUP, |
| 37 | + POLLNVAL, |
66 | 38 | }
|
67 | 39 | }
|
68 | 40 |
|
69 |
| -pub fn poll(fds: &mut [PollFd], timeout: c_int) -> Result<c_int> { |
| 41 | +pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> { |
70 | 42 | let res = unsafe {
|
71 |
| - ffi::poll(fds.as_mut_ptr(), fds.len() as ffi::nfds_t, timeout) |
| 43 | + libc::poll(fds.as_mut_ptr() as *mut libc::pollfd, |
| 44 | + fds.len() as libc::nfds_t, |
| 45 | + timeout) |
72 | 46 | };
|
73 | 47 |
|
74 | 48 | Errno::result(res)
|
|
0 commit comments