diff --git a/CHANGELOG.md b/CHANGELOG.md index bc93a76fd9..f18398efc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/poll.rs b/src/poll.rs index e814337a5f..747e6d8c82 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -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::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! { diff --git a/test/test_poll.rs b/test/test_poll.rs index 0395512ba7..62a4d01189 100644 --- a/test/test_poll.rs +++ b/test/test_poll.rs @@ -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); +}