diff options
author | Homu <homu@barosl.com> | 2016-09-01 04:11:09 +0900 |
---|---|---|
committer | Homu <homu@barosl.com> | 2016-09-01 04:11:09 +0900 |
commit | 9140e623f1309948a00b4d63364ad9d8a9731c41 (patch) | |
tree | 0e65d7de2d1279fbf9642d7a1a04437871308106 | |
parent | 07039357d8d519ab1fe3591b9f9b05fd49ab4195 (diff) | |
parent | f4a52b3640137116fb641a57cd0568721a7f27bc (diff) | |
download | nix-9140e623f1309948a00b4d63364ad9d8a9731c41.zip |
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.
-rw-r--r-- | CHANGELOG.md | 9 | ||||
-rw-r--r-- | src/poll.rs | 92 | ||||
-rw-r--r-- | test/test_poll.rs | 10 |
3 files changed, 45 insertions, 66 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 04e9322f..933a771e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#407](https://github.com/nix-rust/nix/pull/407)) - Added `CpuSet::unset` in `::nix::sched`. ([#402](https://github.com/nix-rust/nix/pull/402)) +- Added constructor method `new()` to `PollFd` in `::nix::poll`, in order to + allow creation of objects, after removing public access to members. + ([#399](https://github.com/nix-rust/nix/pull/399)) +- Added method `revents()` to `PollFd` in `::nix::poll`, in order to provide + read access to formerly public member `revents`. + ([#399](https://github.com/nix-rust/nix/pull/399)) ### Changed - Replaced the reexported integer constants for signals by the enumeration @@ -40,6 +46,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#362](https://github.com/nix-rust/nix/pull/362)) - Type alias `CpuMask` from `::nix::shed`. ([#402](https://github.com/nix-rust/nix/pull/402)) +- Removed public fields from `PollFd` in `::nix::poll`. (See also added method + `revents()`. + ([#399](https://github.com/nix-rust/nix/pull/399)) ### Fixed - Fixed the build problem for NetBSD (Note, that we currently do not support diff --git a/src/poll.rs b/src/poll.rs index 88ca9825..6ba9f5e4 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -1,74 +1,48 @@ -use libc::c_int; +use libc; use {Errno, Result}; -pub use self::ffi::PollFd; -pub use self::ffi::consts::*; - -mod ffi { - use libc::c_int; - pub use self::consts::*; - - #[derive(Clone, Copy, Debug)] - #[repr(C)] - pub struct PollFd { - pub fd: c_int, - pub events: EventFlags, - pub revents: EventFlags - } - - #[cfg(target_os = "linux")] - pub mod consts { - use libc::{c_short, c_ulong}; +#[repr(C)] +#[derive(Clone, Copy)] +pub struct PollFd { + pollfd: libc::pollfd, +} - bitflags! { - flags EventFlags: c_short { - const POLLIN = 0x001, - const POLLPRI = 0x002, - const POLLOUT = 0x004, - const POLLRDNORM = 0x040, - const POLLWRNORM = 0x100, - const POLLRDBAND = 0x080, - const POLLWRBAND = 0x200, - const POLLERR = 0x008, - const POLLHUP = 0x010, - const POLLNVAL = 0x020, - } +impl PollFd { + pub fn new(fd: libc::c_int, events: EventFlags, revents: EventFlags) -> PollFd { + PollFd { + pollfd: libc::pollfd { + fd: fd, + events: events.bits(), + revents: revents.bits(), + }, } - - pub type nfds_t = c_ulong; } - #[cfg(target_os = "macos")] - pub mod consts { - use libc::{c_short, c_uint}; - - bitflags! { - flags EventFlags: c_short { - const POLLIN = 0x0001, - const POLLPRI = 0x0002, - const POLLOUT = 0x0004, - const POLLRDNORM = 0x0040, - const POLLWRNORM = 0x0004, - const POLLRDBAND = 0x0080, - const POLLWRBAND = 0x0100, - const POLLERR = 0x0008, - const POLLHUP = 0x0010, - const POLLNVAL = 0x0020, - } - } - - pub type nfds_t = c_uint; + pub fn revents(&self) -> Option<EventFlags> { + EventFlags::from_bits(self.pollfd.revents) } +} - #[allow(improper_ctypes)] - extern { - pub fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int; +libc_bitflags! { + flags EventFlags: libc::c_short { + POLLIN, + POLLPRI, + POLLOUT, + POLLRDNORM, + POLLWRNORM, + POLLRDBAND, + POLLWRBAND, + POLLERR, + POLLHUP, + POLLNVAL, } } -pub fn poll(fds: &mut [PollFd], timeout: c_int) -> Result<c_int> { +pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> { let res = unsafe { - ffi::poll(fds.as_mut_ptr(), fds.len() as ffi::nfds_t, timeout) + libc::poll(fds.as_mut_ptr() as *mut libc::pollfd, + fds.len() as libc::nfds_t, + timeout) }; Errno::result(res) diff --git a/test/test_poll.rs b/test/test_poll.rs index 54fd4029..13a95d2c 100644 --- a/test/test_poll.rs +++ b/test/test_poll.rs @@ -4,19 +4,15 @@ use nix::unistd::{write, pipe}; #[test] fn test_poll() { let (r, w) = pipe().unwrap(); - let mut fds = [PollFd { - fd: r, - events: POLLIN, - revents: EventFlags::empty() - }]; + let mut fds = [PollFd::new(r, POLLIN, EventFlags::empty())]; let nfds = poll(&mut fds, 100).unwrap(); assert_eq!(nfds, 0); - assert!(!fds[0].revents.contains(POLLIN)); + assert!(!fds[0].revents().unwrap().contains(POLLIN)); write(w, b".").unwrap(); let nfds = poll(&mut fds, 100).unwrap(); assert_eq!(nfds, 1); - assert!(fds[0].revents.contains(POLLIN)); + assert!(fds[0].revents().unwrap().contains(POLLIN)); } |