2 use super::super::conv
::borrowed_fd
;
4 use super::super::fd
::RawFd
;
5 use super::super::fd
::{AsFd, AsRawFd, BorrowedFd, LibcFd}
;
6 use bitflags
::bitflags
;
7 use core
::marker
::PhantomData
;
12 /// `POLL*` flags for use with [`poll`].
14 /// [`poll`]: crate::io::poll
15 pub struct PollFlags
: c
::c_short
{
19 #[cfg(not(target_os = "wasi"))]
20 const PRI
= c
::POLLPRI
;
22 const OUT
= c
::POLLOUT
;
24 #[cfg(not(target_os = "redox"))]
25 const RDNORM
= c
::POLLRDNORM
;
27 #[cfg(not(target_os = "redox"))]
28 const WRNORM
= c
::POLLWRNORM
;
30 #[cfg(not(any(target_os = "redox", target_os = "wasi")))]
31 const RDBAND
= c
::POLLRDBAND
;
33 #[cfg(not(any(target_os = "redox", target_os = "wasi")))]
34 const WRBAND
= c
::POLLWRBAND
;
36 const ERR
= c
::POLLERR
;
38 const HUP
= c
::POLLHUP
;
40 const NVAL
= c
::POLLNVAL
;
43 any(target_os
= "android", target_os
= "linux"),
44 not(any(target_arch
= "sparc", target_arch
= "sparc64"))),
46 const RDHUP
= c
::POLLRDHUP
;
50 /// `struct pollfd`—File descriptor and flags for use with [`poll`].
52 /// [`poll`]: crate::io::poll
53 #[doc(alias = "pollfd")]
55 #[cfg_attr(not(windows), derive(Debug))]
57 pub struct PollFd
<'fd
> {
59 _phantom
: PhantomData
<BorrowedFd
<'fd
>>,
63 impl<'fd
> fmt
::Debug
for PollFd
<'fd
> {
64 fn fmt(&self, fmt
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
65 fmt
.debug_struct("pollfd")
66 .field("fd", &self.pollfd
.fd
)
67 .field("events", &self.pollfd
.events
)
68 .field("revents", &self.pollfd
.revents
)
73 impl<'fd
> PollFd
<'fd
> {
74 /// Constructs a new `PollFd` holding `fd` and `events`.
76 pub fn new
<Fd
: AsFd
>(fd
: &'fd Fd
, events
: PollFlags
) -> Self {
77 Self::from_borrowed_fd(fd
.as_fd(), events
)
80 /// Sets the contained file descriptor to `fd`.
82 pub fn set_fd
<Fd
: AsFd
>(&mut self, fd
: &'fd Fd
) {
83 self.pollfd
.fd
= fd
.as_fd().as_raw_fd() as LibcFd
;
86 /// Clears the ready events.
88 pub fn clear_revents(&mut self) {
89 self.pollfd
.revents
= 0;
92 /// Constructs a new `PollFd` holding `fd` and `events`.
94 /// This is the same as `new`, but can be used to avoid borrowing the
95 /// `BorrowedFd`, which can be tricky in situations where the `BorrowedFd`
98 pub fn from_borrowed_fd(fd
: BorrowedFd
<'fd
>, events
: PollFlags
) -> Self {
102 events
: events
.bits(),
105 _phantom
: PhantomData
,
109 /// Returns the ready events.
111 pub fn revents(&self) -> PollFlags
{
112 // Use `unwrap()` here because in theory we know we know all the bits
113 // the OS might set here, but OS's have added extensions in the past.
114 PollFlags
::from_bits(self.pollfd
.revents
).unwrap()
119 impl<'fd
> AsFd
for PollFd
<'fd
> {
121 fn as_fd(&self) -> BorrowedFd
<'_
> {
122 // SAFETY: Our constructors and `set_fd` require `pollfd.fd` to be
123 // valid for the `fd lifetime.
124 unsafe { BorrowedFd::borrow_raw(self.pollfd.fd) }
129 impl<'fd
> io_lifetimes
::AsSocket
for PollFd
<'fd
> {
131 fn as_socket(&self) -> BorrowedFd
<'_
> {
132 // SAFETY: Our constructors and `set_fd` require `pollfd.fd` to be
133 // valid for the `fd lifetime.
134 unsafe { BorrowedFd::borrow_raw(self.pollfd.fd as RawFd) }