1 | use crate::fd::{AsFd, BorrowedFd}; |
2 | use bitflags::bitflags; |
3 | |
4 | bitflags! { |
5 | /// `POLL*` flags for use with [`poll`]. |
6 | /// |
7 | /// [`poll`]: crate::event::poll |
8 | #[repr (transparent)] |
9 | #[derive (Copy, Clone, Eq, PartialEq, Hash, Debug)] |
10 | pub struct PollFlags: u16 { |
11 | /// `POLLIN` |
12 | const IN = linux_raw_sys::general::POLLIN as u16; |
13 | /// `POLLPRI` |
14 | const PRI = linux_raw_sys::general::POLLPRI as u16; |
15 | /// `POLLOUT` |
16 | const OUT = linux_raw_sys::general::POLLOUT as u16; |
17 | /// `POLLRDNORM` |
18 | const RDNORM = linux_raw_sys::general::POLLRDNORM as u16; |
19 | /// `POLLWRNORM` |
20 | const WRNORM = linux_raw_sys::general::POLLWRNORM as u16; |
21 | /// `POLLRDBAND` |
22 | const RDBAND = linux_raw_sys::general::POLLRDBAND as u16; |
23 | /// `POLLWRBAND` |
24 | const WRBAND = linux_raw_sys::general::POLLWRBAND as u16; |
25 | /// `POLLERR` |
26 | const ERR = linux_raw_sys::general::POLLERR as u16; |
27 | /// `POLLHUP` |
28 | const HUP = linux_raw_sys::general::POLLHUP as u16; |
29 | /// `POLLNVAL` |
30 | const NVAL = linux_raw_sys::general::POLLNVAL as u16; |
31 | /// `POLLRDHUP` |
32 | const RDHUP = linux_raw_sys::general::POLLRDHUP as u16; |
33 | |
34 | /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags> |
35 | const _ = !0; |
36 | } |
37 | } |
38 | |
39 | /// `struct pollfd`—File descriptor and flags for use with [`poll`]. |
40 | /// |
41 | /// [`poll`]: crate::event::poll |
42 | #[doc (alias = "pollfd" )] |
43 | #[repr (C)] |
44 | #[derive (Debug, Clone)] |
45 | pub struct PollFd<'fd> { |
46 | pub(crate) fd: BorrowedFd<'fd>, |
47 | pub(crate) events: u16, |
48 | pub(crate) revents: u16, |
49 | } |
50 | |
51 | impl<'fd> PollFd<'fd> { |
52 | /// Constructs a new `PollFd` holding `fd` and `events`. |
53 | #[inline ] |
54 | pub fn new<Fd: AsFd>(fd: &'fd Fd, events: PollFlags) -> Self { |
55 | Self::from_borrowed_fd(fd.as_fd(), events) |
56 | } |
57 | |
58 | /// Sets the contained file descriptor to `fd`. |
59 | #[inline ] |
60 | pub fn set_fd<Fd: AsFd>(&mut self, fd: &'fd Fd) { |
61 | self.fd = fd.as_fd(); |
62 | } |
63 | |
64 | /// Clears the ready events. |
65 | #[inline ] |
66 | pub fn clear_revents(&mut self) { |
67 | self.revents = 0; |
68 | } |
69 | |
70 | /// Constructs a new `PollFd` holding `fd` and `events`. |
71 | /// |
72 | /// This is the same as `new`, but can be used to avoid borrowing the |
73 | /// `BorrowedFd`, which can be tricky in situations where the `BorrowedFd` |
74 | /// is a temporary. |
75 | #[inline ] |
76 | pub fn from_borrowed_fd(fd: BorrowedFd<'fd>, events: PollFlags) -> Self { |
77 | Self { |
78 | fd, |
79 | events: events.bits(), |
80 | revents: 0, |
81 | } |
82 | } |
83 | |
84 | /// Returns the ready events. |
85 | #[inline ] |
86 | pub fn revents(&self) -> PollFlags { |
87 | // Use `.unwrap()` here because in theory we know we know all the bits |
88 | // the OS might set here, but OS's have added extensions in the past. |
89 | PollFlags::from_bits(self.revents).unwrap() |
90 | } |
91 | } |
92 | |
93 | impl<'fd> AsFd for PollFd<'fd> { |
94 | #[inline ] |
95 | fn as_fd(&self) -> BorrowedFd<'_> { |
96 | self.fd.as_fd() |
97 | } |
98 | } |
99 | |