#![cfg(unix)]
use pin_project::pin_project;
use tokio::io::{Error, ErrorKind::Interrupted, ReadBuf};
use std::{
io,
path::Path,
pin::Pin,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite};
#[pin_project]
#[derive(Debug)]
pub(crate) struct Socket {
#[pin]
#[cfg(unix)]
inner: tokio::net::UnixStream,
}
impl Socket {
#[cfg(unix)]
pub async fn new<P: AsRef<Path>>(path: P) -> Result<Socket, io::Error> {
Ok(Socket {
inner: tokio::net::UnixStream::connect(path).await?,
})
}
}
impl AsyncRead for Socket {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<Result<(), Error>> {
let mut this = self.project();
with_interrupted!(this.inner.as_mut().poll_read(cx, buf))
}
}
impl AsyncWrite for Socket {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8],
) -> Poll<Result<usize, Error>> {
let mut this = self.project();
with_interrupted!(this.inner.as_mut().poll_write(cx, buf))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Error>> {
let mut this = self.project();
with_interrupted!(this.inner.as_mut().poll_flush(cx))
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Error>> {
let mut this = self.project();
with_interrupted!(this.inner.as_mut().poll_shutdown(cx))
}
}