Skip to content

Commit 3e67373

Browse files
committed
Fix #411 - Provide accessors for 'events' in PollFd
Test: `cargo test --test test test_pollfd_events`
1 parent 2142ec3 commit 3e67373

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3636
(#[1511](https://github.com/nix-rust/nix/pull/1511))
3737
- Added `Ipv4RecvErr` and `Ipv6RecvErr` sockopts and associated control messages.
3838
(#[1514](https://github.com/nix-rust/nix/pull/1514))
39+
- Added read/write accessors for 'events' on `PollFd`.
40+
(#[1517](https://github.com/nix-rust/nix/pull/1517))
3941

4042
### Changed
4143

src/poll.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,21 @@ impl PollFd {
3535
}
3636
}
3737

38-
/// Returns the events that occured in the last call to `poll` or `ppoll`.
38+
/// Returns the events that occured in the last call to `poll` or `ppoll`. Will only return
39+
/// `None` if the kernel provides status flags that Nix does not know about.
3940
pub fn revents(self) -> Option<PollFlags> {
4041
PollFlags::from_bits(self.pollfd.revents)
4142
}
43+
44+
/// The events of interest for this `PollFd`.
45+
pub fn events(self) -> PollFlags {
46+
PollFlags::from_bits(self.pollfd.events).unwrap()
47+
}
48+
49+
/// Modify the events of interest for this `PollFd`.
50+
pub fn set_events(&mut self, events: PollFlags) {
51+
self.pollfd.events = events.bits();
52+
}
4253
}
4354

4455
libc_bitflags! {

test/test_poll.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ fn test_ppoll() {
6464
assert_eq!(nfds, 1);
6565
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
6666
}
67+
68+
#[test]
69+
fn test_pollfd_events() {
70+
let mut pfd = PollFd::new(-1, PollFlags::POLLIN);
71+
assert_eq!(pfd.events(), PollFlags::POLLIN);
72+
pfd.set_events(PollFlags::POLLOUT);
73+
assert_eq!(pfd.events(), PollFlags::POLLOUT);
74+
}

0 commit comments

Comments
 (0)