use async_trait::async_trait;
use std::os::unix::io::{AsRawFd, FromRawFd};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::net::{TcpStream, UnixStream};
#[async_trait]
pub trait AsyncStream: AsyncRead + AsyncWrite + Unpin + Send {
async fn try_shutdown(&mut self) -> std::io::Result<()>;
}
#[async_trait]
impl AsyncStream for TcpStream {
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
let std_stream = std::mem::ManuallyDrop::new(unsafe {
std::net::TcpStream::from_raw_fd(self.as_raw_fd())
});
std_stream.shutdown(std::net::Shutdown::Both)
}
}
#[async_trait]
impl AsyncStream for UnixStream {
async fn try_shutdown(&mut self) -> std::io::Result<()> {
self.shutdown().await
}
}
#[async_trait]
impl<IO> AsyncStream for tokio_rustls::client::TlsStream<IO>
where
IO: AsyncStream,
{
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
self.get_mut().0.try_shutdown().await
}
}
#[async_trait]
impl<IO> AsyncStream for tokio_rustls::server::TlsStream<IO>
where
IO: AsyncStream,
{
async fn try_shutdown(&mut self) -> std::io::Result<()> {
let _ = self.shutdown().await;
self.get_mut().0.try_shutdown().await
}
}