use async_trait::async_trait;
use std::os::unix::io::{AsRawFd, FromRawFd};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;
#[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 tokio_rustls::client::TlsStream<TcpStream> {
async fn try_shutdown(&mut self) -> std::io::Result<()> {
self.shutdown().await
}
}
#[async_trait]
impl AsyncStream for tokio_rustls::server::TlsStream<TcpStream> {
async fn try_shutdown(&mut self) -> std::io::Result<()> {
self.shutdown().await
}
}