Skip to content

Commit cc7ddac

Browse files
fwalchrotty
authored andcommitted
PollItem: Support checking associated socket
Adds has_socket and has_fd to PollItem to allow checking which socket a PollItem has been created from. Enables some use cases related to #68.
1 parent 38ddfba commit cc7ddac

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,16 @@ impl<'a> PollItem<'a> {
10551055
pub fn is_error(&self) -> bool {
10561056
(self.revents & POLLERR.bits()) != 0
10571057
}
1058+
1059+
/// Returns true if the polled socket is the given 0MQ socket.
1060+
pub fn has_socket(&self, socket: &Socket) -> bool {
1061+
self.socket == socket.sock
1062+
}
1063+
1064+
/// Returns true if the polled socket is the given file descriptor.
1065+
pub fn has_fd(&self, fd: RawFd) -> bool {
1066+
self.socket.is_null() && self.fd == fd
1067+
}
10581068
}
10591069

10601070
/// Poll for events on multiple sockets.

tests/poll/unix.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn test_pipe_poll() {
1212
pipe_writer(pipe_write);
1313
});
1414
let pipe_item = zmq::PollItem::from_fd(pipe_read, zmq::POLLIN);
15+
assert!(pipe_item.has_fd(pipe_read));
1516

1617
let mut poll_items = [pipe_item];
1718
assert_eq!(zmq::poll(&mut poll_items, 1000).unwrap(), 1);

tests/test.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ test!(test_polling, {
9898
let mut poll_items = vec![receiver.as_poll_item(POLLIN)];
9999
assert_eq!(poll(&mut poll_items, 1000).unwrap(), 1);
100100
assert_eq!(poll_items[0].get_revents(), POLLIN);
101+
assert!(poll_items[0].is_readable());
102+
assert!(!poll_items[0].is_writable());
103+
assert!(!poll_items[0].is_error());
104+
assert!(poll_items[0].has_socket(&receiver));
105+
assert!(!poll_items[0].has_fd(0));
101106
});
102107

103108
test!(test_raw_roundtrip, {

0 commit comments

Comments
 (0)