rustix/backend/libc/event/
poll_fd.rs1use crate::backend::c;
2use crate::backend::conv::borrowed_fd;
3use crate::backend::fd::{AsFd, AsRawFd, BorrowedFd, LibcFd};
4#[cfg(windows)]
5use crate::backend::fd::{AsSocket, RawFd};
6use bitflags::bitflags;
7use core::fmt;
8use core::marker::PhantomData;
9
10bitflags! {
11 #[repr(transparent)]
15 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
16 pub struct PollFlags: c::c_short {
17 const IN = c::POLLIN;
19 #[cfg(not(target_os = "wasi"))]
21 const PRI = c::POLLPRI;
22 const OUT = c::POLLOUT;
24 const RDNORM = c::POLLRDNORM;
26 #[cfg(not(target_os = "l4re"))]
28 const WRNORM = c::POLLWRNORM;
29 #[cfg(not(any(target_os = "l4re", target_os = "wasi")))]
31 const RDBAND = c::POLLRDBAND;
32 #[cfg(not(any(target_os = "l4re", target_os = "wasi")))]
34 const WRBAND = c::POLLWRBAND;
35 const ERR = c::POLLERR;
37 const HUP = c::POLLHUP;
39 #[cfg(not(target_os = "espidf"))]
41 const NVAL = c::POLLNVAL;
42 #[cfg(any(
44 target_os = "freebsd",
45 target_os = "illumos",
46 all(
47 linux_kernel,
48 not(any(target_arch = "sparc", target_arch = "sparc64"))
49 ),
50 ))]
51 const RDHUP = c::POLLRDHUP;
52
53 const _ = !0;
55 }
56}
57
58#[doc(alias = "pollfd")]
62#[derive(Clone)]
63#[repr(transparent)]
64pub struct PollFd<'fd> {
65 pollfd: c::pollfd,
66 _phantom: PhantomData<BorrowedFd<'fd>>,
67}
68
69impl<'fd> fmt::Debug for PollFd<'fd> {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.debug_struct("PollFd")
72 .field("fd", &self.pollfd.fd)
73 .field("events", &self.pollfd.events)
74 .field("revents", &self.pollfd.revents)
75 .finish()
76 }
77}
78
79impl<'fd> PollFd<'fd> {
80 #[inline]
82 pub fn new<Fd: AsFd>(fd: &'fd Fd, events: PollFlags) -> Self {
83 Self::from_borrowed_fd(fd.as_fd(), events)
84 }
85
86 #[inline]
88 pub fn set_fd<Fd: AsFd>(&mut self, fd: &'fd Fd) {
89 self.pollfd.fd = fd.as_fd().as_raw_fd() as LibcFd;
90 }
91
92 #[inline]
94 pub fn clear_revents(&mut self) {
95 self.pollfd.revents = 0;
96 }
97
98 #[inline]
104 pub fn from_borrowed_fd(fd: BorrowedFd<'fd>, events: PollFlags) -> Self {
105 Self {
106 pollfd: c::pollfd {
107 fd: borrowed_fd(fd),
108 events: events.bits(),
109 revents: 0,
110 },
111 _phantom: PhantomData,
112 }
113 }
114
115 #[inline]
117 pub fn revents(&self) -> PollFlags {
118 PollFlags::from_bits(self.pollfd.revents).unwrap()
121 }
122}
123
124#[cfg(not(windows))]
125impl<'fd> AsFd for PollFd<'fd> {
126 #[inline]
127 fn as_fd(&self) -> BorrowedFd<'_> {
128 unsafe { BorrowedFd::borrow_raw(self.pollfd.fd) }
131 }
132}
133
134#[cfg(windows)]
135impl<'fd> AsSocket for PollFd<'fd> {
136 #[inline]
137 fn as_socket(&self) -> BorrowedFd<'_> {
138 unsafe { BorrowedFd::borrow_raw(self.pollfd.fd as RawFd) }
141 }
142}