use io_extras::os::rustix::{AsRawFd, AsRawReadWriteFd, AsReadWriteFd, RawFd};
use io_lifetimes::{AsFd, BorrowedFd};
use std::fmt::{self, Debug};
use std::io::IoSlice;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{self, AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::UnixStream;
#[repr(transparent)]
pub struct TokioSocketpairStream(UnixStream);
impl TokioSocketpairStream {
#[inline]
pub fn num_ready_bytes(&self) -> io::Result<u64> {
Ok(rustix::io::ioctl_fionread(self)?)
}
}
#[inline]
pub async fn tokio_socketpair_stream() -> io::Result<(TokioSocketpairStream, TokioSocketpairStream)>
{
UnixStream::pair().map(|(a, b)| (TokioSocketpairStream(a), TokioSocketpairStream(b)))
}
impl AsyncRead for TokioSocketpairStream {
#[inline]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}
impl AsyncWrite for TokioSocketpairStream {
#[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_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}
impl AsRawFd for TokioSocketpairStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl AsFd for TokioSocketpairStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
impl AsRawReadWriteFd for TokioSocketpairStream {
#[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 TokioSocketpairStream {
#[inline]
fn as_read_fd(&self) -> BorrowedFd<'_> {
self.as_fd()
}
#[inline]
fn as_write_fd(&self) -> BorrowedFd<'_> {
self.as_fd()
}
}
impl Debug for TokioSocketpairStream {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("TokioSocketpairStream")
.field("raw_fd", &self.0.as_raw_fd())
.finish()
}
}