Skip to content

Commit

Permalink
Fix #411 - Provide accessors for 'events' in PollFd
Browse files Browse the repository at this point in the history
Test: `cargo test --test test test_pollfd_events`
  • Loading branch information
cemeyer committed Sep 7, 2021
1 parent 2142ec3 commit 3e67373
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1511](https://github.com/nix-rust/nix/pull/1511))
- Added `Ipv4RecvErr` and `Ipv6RecvErr` sockopts and associated control messages.
(#[1514](https://github.com/nix-rust/nix/pull/1514))
- Added read/write accessors for 'events' on `PollFd`.
(#[1517](https://github.com/nix-rust/nix/pull/1517))

### Changed

Expand Down
13 changes: 12 additions & 1 deletion src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ impl PollFd {
}
}

/// Returns the events that occured in the last call to `poll` or `ppoll`.
/// Returns the events that occured in the last call to `poll` or `ppoll`. Will only return
/// `None` if the kernel provides status flags that Nix does not know about.
pub fn revents(self) -> Option<PollFlags> {
PollFlags::from_bits(self.pollfd.revents)
}

/// The events of interest for this `PollFd`.
pub fn events(self) -> PollFlags {
PollFlags::from_bits(self.pollfd.events).unwrap()
}

/// Modify the events of interest for this `PollFd`.
pub fn set_events(&mut self, events: PollFlags) {
self.pollfd.events = events.bits();
}
}

libc_bitflags! {
Expand Down
8 changes: 8 additions & 0 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ fn test_ppoll() {
assert_eq!(nfds, 1);
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
}

#[test]
fn test_pollfd_events() {
let mut pfd = PollFd::new(-1, PollFlags::POLLIN);
assert_eq!(pfd.events(), PollFlags::POLLIN);
pfd.set_events(PollFlags::POLLOUT);
assert_eq!(pfd.events(), PollFlags::POLLOUT);
}

0 comments on commit 3e67373

Please sign in to comment.