| 1 |
use crate::backend::c; |
| 2 |
use crate::backend::conv::borrowed_fd; |
| 3 |
use crate::backend::fd::{AsFd, AsRawFd, BorrowedFd, LibcFd}; |
| 4 |
#[cfg(windows)] |
| 5 |
use crate::backend::fd::{AsSocket, RawFd}; |
| 6 |
use bitflags::bitflags; |
| 7 |
use core::fmt; |
| 8 |
use core::marker::PhantomData; |
| 9 |
|
| 10 |
bitflags! { |
| 11 |
/// `POLL*` flags for use with [`poll`]. |
| 12 |
/// |
| 13 |
/// [`poll`]: crate::event::poll |
| 14 |
#[repr(transparent)] |
| 15 |
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] |
| 16 |
pub struct PollFlags: c::c_short { |
| 17 |
/// `POLLIN` |
| 18 |
const IN = c::POLLIN; |
| 19 |
/// `POLLPRI` |
| 20 |
#[cfg(not(target_os = "wasi"))] |
| 21 |
const PRI = c::POLLPRI; |
| 22 |
/// `POLLOUT` |
| 23 |
const OUT = c::POLLOUT; |
| 24 |
/// `POLLRDNORM` |
| 25 |
const RDNORM = c::POLLRDNORM; |
| 26 |
/// `POLLWRNORM` |
| 27 |
#[cfg(not(target_os = "l4re"))] |
| 28 |
const WRNORM = c::POLLWRNORM; |
| 29 |
/// `POLLRDBAND` |
| 30 |
#[cfg(not(any(target_os = "l4re", target_os = "wasi")))] |
| 31 |
const RDBAND = c::POLLRDBAND; |
| 32 |
/// `POLLWRBAND` |
| 33 |
#[cfg(not(any(target_os = "l4re", target_os = "wasi")))] |
| 34 |
const WRBAND = c::POLLWRBAND; |
| 35 |
/// `POLLERR` |
| 36 |
const ERR = c::POLLERR; |
| 37 |
/// `POLLHUP` |
| 38 |
const HUP = c::POLLHUP; |
| 39 |
/// `POLLNVAL` |
| 40 |
#[cfg(not(target_os = "espidf"))] |
| 41 |
const NVAL = c::POLLNVAL; |
| 42 |
/// `POLLRDHUP` |
| 43 |
#[cfg(all( |
| 44 |
linux_kernel, |
| 45 |
not(any(target_arch = "sparc", target_arch = "sparc64"))), |
| 46 |
)] |
| 47 |
const RDHUP = c::POLLRDHUP; |
| 48 |
|
| 49 |
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags> |
| 50 |
const _ = !0; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// `struct pollfd`—File descriptor and flags for use with [`poll`]. |
| 55 |
/// |
| 56 |
/// [`poll`]: crate::event::poll |
| 57 |
#[doc(alias = "pollfd")] |
| 58 |
#[derive(Clone)] |
| 59 |
#[repr(transparent)] |
| 60 |
pub struct PollFd<'fd> { |
| 61 |
pollfd: c::pollfd, |
| 62 |
_phantom: PhantomData<BorrowedFd<'fd>>, |
| 63 |
} |
| 64 |
|
| 65 |
impl<'fd> fmt::Debug for PollFd<'fd> { |
| 66 |
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 67 |
fmt.debug_struct("PollFd") |
| 68 |
.field("fd", &self.pollfd.fd) |
| 69 |
.field("events", &self.pollfd.events) |
| 70 |
.field("revents", &self.pollfd.revents) |
| 71 |
.finish() |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl<'fd> PollFd<'fd> { |
| 76 |
/// Constructs a new `PollFd` holding `fd` and `events`. |
| 77 |
#[inline] |
| 78 |
pub fn new<Fd: AsFd>(fd: &'fd Fd, events: PollFlags) -> Self { |
| 79 |
Self::from_borrowed_fd(fd.as_fd(), events) |
| 80 |
} |
| 81 |
|
| 82 |
/// Sets the contained file descriptor to `fd`. |
| 83 |
#[inline] |
| 84 |
pub fn set_fd<Fd: AsFd>(&mut self, fd: &'fd Fd) { |
| 85 |
self.pollfd.fd = fd.as_fd().as_raw_fd() as LibcFd; |
| 86 |
} |
| 87 |
|
| 88 |
/// Clears the ready events. |
| 89 |
#[inline] |
| 90 |
pub fn clear_revents(&mut self) { |
| 91 |
self.pollfd.revents = 0; |
| 92 |
} |
| 93 |
|
| 94 |
/// Constructs a new `PollFd` holding `fd` and `events`. |
| 95 |
/// |
| 96 |
/// This is the same as `new`, but can be used to avoid borrowing the |
| 97 |
/// `BorrowedFd`, which can be tricky in situations where the `BorrowedFd` |
| 98 |
/// is a temporary. |
| 99 |
#[inline] |
| 100 |
pub fn from_borrowed_fd(fd: BorrowedFd<'fd>, events: PollFlags) -> Self { |
| 101 |
Self { |
| 102 |
pollfd: c::pollfd { |
| 103 |
fd: borrowed_fd(fd), |
| 104 |
events: events.bits(), |
| 105 |
revents: 0, |
| 106 |
}, |
| 107 |
_phantom: PhantomData, |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/// Returns the ready events. |
| 112 |
#[inline] |
| 113 |
pub fn revents(&self) -> PollFlags { |
| 114 |
// Use `.unwrap()` here because in theory we know we know all the bits |
| 115 |
// the OS might set here, but OS's have added extensions in the past. |
| 116 |
PollFlags::from_bits(self.pollfd.revents).unwrap() |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(not(windows))] |
| 121 |
impl<'fd> AsFd for PollFd<'fd> { |
| 122 |
#[inline] |
| 123 |
fn as_fd(&self) -> BorrowedFd<'_> { |
| 124 |
// SAFETY: Our constructors and `set_fd` require `pollfd.fd` to be |
| 125 |
// valid for the `'fd` lifetime. |
| 126 |
unsafe { BorrowedFd::borrow_raw(self.pollfd.fd) } |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(windows)] |
| 131 |
impl<'fd> AsSocket for PollFd<'fd> { |
| 132 |
#[inline] |
| 133 |
fn as_socket(&self) -> BorrowedFd<'_> { |
| 134 |
// SAFETY: Our constructors and `set_fd` require `pollfd.fd` to be |
| 135 |
// valid for the `'fd` lifetime. |
| 136 |
unsafe { BorrowedFd::borrow_raw(self.pollfd.fd as RawFd) } |
| 137 |
} |
| 138 |
} |