use crate::fd::{AsFd, BorrowedFd};
use bitflags::bitflags;
bitflags! {
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct PollFlags: u16 {
const IN = linux_raw_sys::general::POLLIN as u16;
const PRI = linux_raw_sys::general::POLLPRI as u16;
const OUT = linux_raw_sys::general::POLLOUT as u16;
const RDNORM = linux_raw_sys::general::POLLRDNORM as u16;
const WRNORM = linux_raw_sys::general::POLLWRNORM as u16;
const RDBAND = linux_raw_sys::general::POLLRDBAND as u16;
const WRBAND = linux_raw_sys::general::POLLWRBAND as u16;
const ERR = linux_raw_sys::general::POLLERR as u16;
const HUP = linux_raw_sys::general::POLLHUP as u16;
const NVAL = linux_raw_sys::general::POLLNVAL as u16;
const RDHUP = linux_raw_sys::general::POLLRDHUP as u16;
const _ = !0;
}
}
#[doc(alias = "pollfd")]
#[repr(C)]
#[derive(Debug, Clone)]
pub struct PollFd<'fd> {
pub(crate) fd: BorrowedFd<'fd>,
pub(crate) events: u16,
pub(crate) revents: u16,
}
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.fd = fd.as_fd();
}
#[inline]
pub fn clear_revents(&mut self) {
self.revents = 0;
}
#[inline]
pub fn from_borrowed_fd(fd: BorrowedFd<'fd>, events: PollFlags) -> Self {
Self {
fd,
events: events.bits(),
revents: 0,
}
}
#[inline]
pub fn revents(&self) -> PollFlags {
PollFlags::from_bits(self.revents).unwrap()
}
}
impl<'fd> AsFd for PollFd<'fd> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.fd.as_fd()
}
}