cfg_select! {
windows => {
#[path = "windows.rs"]
mod sys;
}
unix => {
#[path = "unix.rs"]
mod sys;
}
_ => {}
}
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, RawSocket};
use std::{
future::poll_fn,
io,
ops::Deref,
pin::Pin,
task::{Context, Poll},
};
use compio_buf::IntoInner;
use compio_driver::{AsFd, AsRawFd, BorrowedFd, RawFd, SharedFd, ToSharedFd};
#[derive(Debug)]
pub struct PollFd<T: AsFd>(sys::PollFd<T>);
impl<T: AsFd> PollFd<T> {
pub fn new(source: T) -> io::Result<Self> {
Self::from_shared_fd(SharedFd::new(source))
}
pub fn from_shared_fd(inner: SharedFd<T>) -> io::Result<Self> {
Ok(Self(sys::PollFd::new(inner)?))
}
}
impl<T: AsFd + 'static> PollFd<T> {
pub async fn accept_ready(&self) -> io::Result<()> {
poll_fn(|cx| self.poll_accept_ready(cx)).await
}
pub async fn connect_ready(&self) -> io::Result<()> {
poll_fn(|cx| self.poll_connect_ready(cx)).await
}
pub async fn read_ready(&self) -> io::Result<()> {
poll_fn(|cx| self.poll_read_ready(cx)).await
}
pub async fn write_ready(&self) -> io::Result<()> {
poll_fn(|cx| self.poll_write_ready(cx)).await
}
pub fn poll_accept_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> {
self.0.poll_accept_ready(cx)
}
pub fn poll_connect_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> {
self.0.poll_connect_ready(cx)
}
pub fn poll_read_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> {
self.0.poll_read_ready(cx)
}
pub fn poll_write_ready(&self, cx: &mut Context) -> Poll<io::Result<()>> {
self.0.poll_write_ready(cx)
}
pub fn poll_accept_with<R>(
&self,
cx: &mut Context,
mut f: impl FnMut(&T) -> io::Result<R>,
) -> Poll<io::Result<R>> {
loop {
match f(&self.0) {
Ok(result) => break Poll::Ready(Ok(result)),
Err(e) if is_would_block(&e) => {
std::task::ready!(self.poll_accept_ready(cx))?;
}
Err(e) => break Poll::Ready(Err(e)),
}
}
}
pub fn poll_read_with<R>(
&self,
cx: &mut Context,
mut f: impl FnMut(&T) -> io::Result<R>,
) -> Poll<io::Result<R>> {
loop {
match f(&self.0) {
Ok(result) => break Poll::Ready(Ok(result)),
Err(e) if is_would_block(&e) => {
std::task::ready!(self.poll_read_ready(cx))?;
}
Err(e) => break Poll::Ready(Err(e)),
}
}
}
pub fn poll_write_with<R>(
&self,
cx: &mut Context,
mut f: impl FnMut(&T) -> io::Result<R>,
) -> Poll<io::Result<R>> {
loop {
match f(&self.0) {
Ok(result) => break Poll::Ready(Ok(result)),
Err(e) if is_would_block(&e) => {
std::task::ready!(self.poll_write_ready(cx))?;
}
Err(e) => break Poll::Ready(Err(e)),
}
}
}
}
impl<T: AsFd + 'static> PollFd<T>
where
for<'a> &'a T: std::io::Read,
{
pub fn poll_read(&self, cx: &mut Context, buf: &mut [u8]) -> Poll<io::Result<usize>> {
self.poll_read_with(cx, |fd| std::io::Read::read(&mut &*fd, buf))
}
#[cfg(feature = "read_buf")]
pub fn poll_read_buf(
&self,
cx: &mut Context,
mut buf: std::io::BorrowedCursor<u8>,
) -> Poll<io::Result<()>> {
self.poll_read_with(cx, |fd| std::io::Read::read_buf(&mut &*fd, buf.reborrow()))
}
}
impl<T: AsFd + 'static> PollFd<T>
where
for<'a> &'a T: std::io::Write,
{
pub fn poll_write(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
self.poll_write_with(cx, |fd| std::io::Write::write(&mut &*fd, buf))
}
}
impl<T: AsFd> IntoInner for PollFd<T> {
type Inner = SharedFd<T>;
fn into_inner(self) -> Self::Inner {
self.0.into_inner()
}
}
impl<T: AsFd> ToSharedFd<T> for PollFd<T> {
fn to_shared_fd(&self) -> SharedFd<T> {
self.0.to_shared_fd()
}
}
impl<T: AsFd> AsFd for PollFd<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
impl<T: AsFd> AsRawFd for PollFd<T> {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
#[cfg(windows)]
impl<T: AsFd + AsRawSocket> AsRawSocket for PollFd<T> {
fn as_raw_socket(&self) -> RawSocket {
self.0.as_raw_socket()
}
}
impl<T: AsFd> Deref for PollFd<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn is_would_block(e: &io::Error) -> bool {
#[cfg(unix)]
{
e.kind() == io::ErrorKind::WouldBlock || e.raw_os_error() == Some(libc::EINPROGRESS)
}
#[cfg(not(unix))]
{
e.kind() == io::ErrorKind::WouldBlock
}
}
impl<T: AsFd + 'static> futures_util::AsyncRead for &PollFd<T>
where
for<'a> &'a T: std::io::Read,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
(*self).poll_read(cx, buf)
}
}
impl<T: AsFd + 'static> futures_util::AsyncRead for PollFd<T>
where
for<'a> &'a T: std::io::Read,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
(*self).poll_read(cx, buf)
}
}
impl<T: AsFd + 'static> futures_util::AsyncWrite for &PollFd<T>
where
for<'a> &'a T: std::io::Write,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
(*self).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}
impl<T: AsFd + 'static> futures_util::AsyncWrite for PollFd<T>
where
for<'a> &'a T: std::io::Write,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
(*self).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}