Skip to content

Commit 9140e62

Browse files
committed
Auto merge of #399 - fiveop:less_ffi_poll, r=posborne
Use libc in poll.rs I'll add a change log commit, once this is reviewed.
2 parents 0703935 + f4a52b3 commit 9140e62

File tree

3 files changed

+45
-66
lines changed

3 files changed

+45
-66
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2323
([#407](https://github.com/nix-rust/nix/pull/407))
2424
- Added `CpuSet::unset` in `::nix::sched`.
2525
([#402](https://github.com/nix-rust/nix/pull/402))
26+
- Added constructor method `new()` to `PollFd` in `::nix::poll`, in order to
27+
allow creation of objects, after removing public access to members.
28+
([#399](https://github.com/nix-rust/nix/pull/399))
29+
- Added method `revents()` to `PollFd` in `::nix::poll`, in order to provide
30+
read access to formerly public member `revents`.
31+
([#399](https://github.com/nix-rust/nix/pull/399))
2632

2733
### Changed
2834
- Replaced the reexported integer constants for signals by the enumeration
@@ -40,6 +46,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4046
([#362](https://github.com/nix-rust/nix/pull/362))
4147
- Type alias `CpuMask` from `::nix::shed`.
4248
([#402](https://github.com/nix-rust/nix/pull/402))
49+
- Removed public fields from `PollFd` in `::nix::poll`. (See also added method
50+
`revents()`.
51+
([#399](https://github.com/nix-rust/nix/pull/399))
4352

4453
### Fixed
4554
- Fixed the build problem for NetBSD (Note, that we currently do not support

src/poll.rs

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,48 @@
1-
use libc::c_int;
1+
use libc;
22
use {Errno, Result};
33

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+
}
229

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+
},
3618
}
37-
38-
pub type nfds_t = c_ulong;
3919
}
4020

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)
6123
}
24+
}
6225

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,
6638
}
6739
}
6840

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> {
7042
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)
7246
};
7347

7448
Errno::result(res)

test/test_poll.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ use nix::unistd::{write, pipe};
44
#[test]
55
fn test_poll() {
66
let (r, w) = pipe().unwrap();
7-
let mut fds = [PollFd {
8-
fd: r,
9-
events: POLLIN,
10-
revents: EventFlags::empty()
11-
}];
7+
let mut fds = [PollFd::new(r, POLLIN, EventFlags::empty())];
128

139
let nfds = poll(&mut fds, 100).unwrap();
1410
assert_eq!(nfds, 0);
15-
assert!(!fds[0].revents.contains(POLLIN));
11+
assert!(!fds[0].revents().unwrap().contains(POLLIN));
1612

1713
write(w, b".").unwrap();
1814

1915
let nfds = poll(&mut fds, 100).unwrap();
2016
assert_eq!(nfds, 1);
21-
assert!(fds[0].revents.contains(POLLIN));
17+
assert!(fds[0].revents().unwrap().contains(POLLIN));
2218
}

0 commit comments

Comments
 (0)