use async_std::io::{self, IoSlice, IoSliceMut, Read, Write};
use async_std::os::unix::net::UnixStream;
use io_extras::os::rustix::{
AsRawFd, AsRawReadWriteFd, AsReadWriteFd, FromRawFd, IntoRawFd, RawFd,
};
use io_lifetimes::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
use std::fmt::{self, Debug};
use std::pin::Pin;
use std::task::{Context, Poll};
#[repr(transparent)]
#[derive(Clone)]
pub struct AsyncStdSocketpairStream(UnixStream);
impl AsyncStdSocketpairStream {
#[inline]
pub fn num_ready_bytes(&self) -> io::Result<u64> {
Ok(rustix::io::ioctl_fionread(self)?)
}
}
#[inline]
pub async fn async_std_socketpair_stream(
) -> io::Result<(AsyncStdSocketpairStream, AsyncStdSocketpairStream)> {
UnixStream::pair().map(|(a, b)| (AsyncStdSocketpairStream(a), AsyncStdSocketpairStream(b)))
}
impl Read for AsyncStdSocketpairStream {
#[inline]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
#[inline]
fn poll_read_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_read_vectored(cx, bufs)
}
}
impl Write for AsyncStdSocketpairStream {
#[inline]
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.0).poll_write_vectored(cx, bufs)
}
#[inline]
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}
#[inline]
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_close(cx)
}
}
impl AsRawFd for AsyncStdSocketpairStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl AsFd for AsyncStdSocketpairStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
impl IntoRawFd for AsyncStdSocketpairStream {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_raw_fd()
}
}
impl From<AsyncStdSocketpairStream> for OwnedFd {
#[inline]
fn from(stream: AsyncStdSocketpairStream) -> OwnedFd {
stream.0.into()
}
}
impl FromRawFd for AsyncStdSocketpairStream {
#[inline]
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
Self(UnixStream::from_raw_fd(raw_fd))
}
}
impl From<OwnedFd> for AsyncStdSocketpairStream {
#[inline]
fn from(fd: OwnedFd) -> Self {
Self(UnixStream::from(fd))
}
}
impl AsRawReadWriteFd for AsyncStdSocketpairStream {
#[inline]
fn as_raw_read_fd(&self) -> RawFd {
self.as_raw_fd()
}
#[inline]
fn as_raw_write_fd(&self) -> RawFd {
self.as_raw_fd()
}
}
impl AsReadWriteFd for AsyncStdSocketpairStream {
#[inline]
fn as_read_fd(&self) -> BorrowedFd<'_> {
self.as_fd()
}
#[inline]
fn as_write_fd(&self) -> BorrowedFd<'_> {
self.as_fd()
}
}
impl Debug for AsyncStdSocketpairStream {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("AsyncStdSocketpairStream")
.field("raw_fd", &self.0.as_raw_fd())
.finish()
}
}