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 #411 - Provide accessors for 'events' in PollFd #1517

Merged
merged 1 commit into from
Sep 20, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1531](https://github.com/nix-rust/nix/pull/1531))
- Added `MAP_ANONYMOUS` for all operating systems.
(#[1534](https://github.com/nix-rust/nix/pull/1534))
- 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();
}
}

impl AsRawFd for PollFd {
Expand Down
8 changes: 8 additions & 0 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ fn test_pollfd_fd() {
let pfd = PollFd::new(0x1234, PollFlags::empty());
assert_eq!(pfd.as_raw_fd(), 0x1234);
}

#[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);
}