use super::super::c;
use super::super::conv::borrowed_fd;
#[cfg(windows)]
use super::super::fd::RawFd;
use super::super::fd::{AsFd, AsRawFd, BorrowedFd, LibcFd};
use bitflags::bitflags;
use core::marker::PhantomData;
#[cfg(windows)]
use std::fmt;
bitflags! {
pub struct PollFlags: c::c_short {
const IN = c::POLLIN;
#[cfg(not(target_os = "wasi"))]
const PRI = c::POLLPRI;
const OUT = c::POLLOUT;
#[cfg(not(target_os = "redox"))]
const RDNORM = c::POLLRDNORM;
#[cfg(not(target_os = "redox"))]
const WRNORM = c::POLLWRNORM;
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
const RDBAND = c::POLLRDBAND;
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
const WRBAND = c::POLLWRBAND;
const ERR = c::POLLERR;
const HUP = c::POLLHUP;
const NVAL = c::POLLNVAL;
#[cfg(all(
any(target_os = "android", target_os = "linux"),
not(any(target_arch = "sparc", target_arch = "sparc64"))),
)]
const RDHUP = c::POLLRDHUP;
}
}
#[doc(alias = "pollfd")]
#[derive(Clone)]
#[cfg_attr(not(windows), derive(Debug))]
#[repr(transparent)]
pub struct PollFd<'fd> {
pollfd: c::pollfd,
_phantom: PhantomData<BorrowedFd<'fd>>,
}
#[cfg(windows)]
impl<'fd> fmt::Debug for PollFd<'fd> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("pollfd")
.field("fd", &self.pollfd.fd)
.field("events", &self.pollfd.events)
.field("revents", &self.pollfd.revents)
.finish()
}
}
impl<'fd> PollFd<'fd> {
#[inline]
pub fn new<Fd: AsFd>(fd: &'fd Fd, events: PollFlags) -> Self {
Self::from_borrowed_fd(fd.as_fd(), events)
}
#[inline]
pub fn set_fd<Fd: AsFd>(&mut self, fd: &'fd Fd) {
self.pollfd.fd = fd.as_fd().as_raw_fd() as LibcFd;
}
#[inline]
pub fn clear_revents(&mut self) {
self.pollfd.revents = 0;
}
#[inline]
pub fn from_borrowed_fd(fd: BorrowedFd<'fd>, events: PollFlags) -> Self {
Self {
pollfd: c::pollfd {
fd: borrowed_fd(fd),
events: events.bits(),
revents: 0,
},
_phantom: PhantomData,
}
}
#[inline]
pub fn revents(&self) -> PollFlags {
PollFlags::from_bits(self.pollfd.revents).unwrap()
}
}
#[cfg(not(windows))]
impl<'fd> AsFd for PollFd<'fd> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.pollfd.fd) }
}
}
#[cfg(windows)]
impl<'fd> io_lifetimes::AsSocket for PollFd<'fd> {
#[inline]
fn as_socket(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.pollfd.fd as RawFd) }
}
}