Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #317 - Some event flags renaming #1024

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Add IP_RECVIF & IP_RECVDSTADDR. Enable IP_PKTINFO and IP6_PKTINFO on netbsd/openbsd.
([#1002](https://github.com/nix-rust/nix/pull/1002))
### Changed
- `PollFd` event flags renamed to `PollFlags` ([#1024](https://github.com/nix-rust/nix/pull/1024/))
### Fixed
### Removed

Expand Down
14 changes: 7 additions & 7 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ pub struct PollFd {
impl PollFd {
/// Creates a new `PollFd` specifying the events of interest
/// for a given file descriptor.
pub fn new(fd: RawFd, events: EventFlags) -> PollFd {
pub fn new(fd: RawFd, events: PollFlags) -> PollFd {
PollFd {
pollfd: libc::pollfd {
fd: fd,
events: events.bits(),
revents: EventFlags::empty().bits(),
revents: PollFlags::empty().bits(),
},
}
}

/// Returns the events that occured in the last call to `poll` or `ppoll`.
pub fn revents(&self) -> Option<EventFlags> {
EventFlags::from_bits(self.pollfd.revents)
pub fn revents(&self) -> Option<PollFlags> {
PollFlags::from_bits(self.pollfd.revents)
}
}

Expand All @@ -48,11 +48,11 @@ impl fmt::Debug for PollFd {
let pfd = self.pollfd;
let mut ds = f.debug_struct("PollFd");
ds.field("fd", &pfd.fd);
match EventFlags::from_bits(pfd.events) {
match PollFlags::from_bits(pfd.events) {
None => ds.field("events", &pfd.events),
Some(ef) => ds.field("events", &ef),
};
match EventFlags::from_bits(pfd.revents) {
match PollFlags::from_bits(pfd.revents) {
None => ds.field("revents", &pfd.revents),
Some(ef) => ds.field("revents", &ef),
};
Expand All @@ -62,7 +62,7 @@ impl fmt::Debug for PollFd {

libc_bitflags! {
/// These flags define the different events that can be monitored by `poll` and `ppoll`
pub struct EventFlags: libc::c_short {
pub struct PollFlags: libc::c_short {
/// There is data to read.
POLLIN;
/// There is some exceptional condition on the file descriptor.
Expand Down
20 changes: 10 additions & 10 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use nix::poll::{EventFlags, poll, PollFd};
use nix::poll::{PollFlags, poll, PollFd};
use nix::unistd::{write, pipe, close};

#[test]
fn test_poll() {
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(r, EventFlags::POLLIN)];
let mut fds = [PollFd::new(r, PollFlags::POLLIN)];

// Poll an idle pipe. Should timeout
let nfds = poll(&mut fds, 100).unwrap();
assert_eq!(nfds, 0);
assert!(!fds[0].revents().unwrap().contains(EventFlags::POLLIN));
assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));

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

// Poll a readable pipe. Should return an event.
let nfds = poll(&mut fds, 100).unwrap();
assert_eq!(nfds, 1);
assert!(fds[0].revents().unwrap().contains(EventFlags::POLLIN));
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
}

#[test]
fn test_poll_debug() {
assert_eq!(format!("{:?}", PollFd::new(0, EventFlags::empty())),
assert_eq!(format!("{:?}", PollFd::new(0, PollFlags::empty())),
"PollFd { fd: 0, events: (empty), revents: (empty) }");
assert_eq!(format!("{:?}", PollFd::new(1, EventFlags::POLLIN)),
assert_eq!(format!("{:?}", PollFd::new(1, PollFlags::POLLIN)),
"PollFd { fd: 1, events: POLLIN, revents: (empty) }");

// Testing revents requires doing some I/O
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(r, EventFlags::POLLIN)];
let mut fds = [PollFd::new(r, PollFlags::POLLIN)];
write(w, b" ").unwrap();
close(w).unwrap();
poll(&mut fds, -1).unwrap();
Expand All @@ -52,17 +52,17 @@ fn test_ppoll() {

let timeout = TimeSpec::milliseconds(1);
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(r, EventFlags::POLLIN)];
let mut fds = [PollFd::new(r, PollFlags::POLLIN)];

// Poll an idle pipe. Should timeout
let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap();
assert_eq!(nfds, 0);
assert!(!fds[0].revents().unwrap().contains(EventFlags::POLLIN));
assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));

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

// Poll a readable pipe. Should return an event.
let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap();
assert_eq!(nfds, 1);
assert!(fds[0].revents().unwrap().contains(EventFlags::POLLIN));
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
}