use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;
#[cfg(unix)]
use tokio::net::UnixStream;
#[cfg(windows)]
use tokio::net::windows::named_pipe::NamedPipeClient;
#[derive(Debug)]
pub enum AsyncStream {
Tcp(TcpStream),
#[cfg(unix)]
Unix(UnixStream),
#[cfg(windows)]
NamedPipe(NamedPipeClient),
}
impl AsyncStream {
pub fn tcp(stream: TcpStream) -> Self {
AsyncStream::Tcp(stream)
}
#[cfg(unix)]
pub fn unix(stream: UnixStream) -> Self {
AsyncStream::Unix(stream)
}
pub fn is_tcp(&self) -> bool {
matches!(self, AsyncStream::Tcp(_))
}
#[cfg(unix)]
pub fn is_unix(&self) -> bool {
matches!(self, AsyncStream::Unix(_))
}
#[cfg(windows)]
pub fn named_pipe(client: NamedPipeClient) -> Self {
AsyncStream::NamedPipe(client)
}
#[cfg(windows)]
pub fn is_named_pipe(&self) -> bool {
matches!(self, AsyncStream::NamedPipe(_))
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
match self {
AsyncStream::Tcp(stream) => stream.set_nodelay(nodelay),
#[cfg(unix)]
AsyncStream::Unix(_) => Ok(()), #[cfg(windows)]
AsyncStream::NamedPipe(_) => Ok(()), }
}
pub fn local_addr_string(&self) -> String {
match self {
AsyncStream::Tcp(stream) => stream
.local_addr()
.map_or_else(|_| "unknown".to_string(), |a| a.to_string()),
#[cfg(unix)]
AsyncStream::Unix(stream) => stream
.local_addr()
.ok()
.and_then(|a| a.as_pathname().map(|p| p.display().to_string()))
.unwrap_or_else(|| "unix-socket".to_string()),
#[cfg(windows)]
AsyncStream::NamedPipe(_) => "named-pipe".to_string(),
}
}
pub fn peer_addr_string(&self) -> String {
match self {
AsyncStream::Tcp(stream) => stream
.peer_addr()
.map_or_else(|_| "unknown".to_string(), |a| a.to_string()),
#[cfg(unix)]
AsyncStream::Unix(stream) => stream
.peer_addr()
.ok()
.and_then(|a| a.as_pathname().map(|p| p.display().to_string()))
.unwrap_or_else(|| "unix-socket".to_string()),
#[cfg(windows)]
AsyncStream::NamedPipe(_) => "named-pipe".to_string(),
}
}
}
impl AsyncRead for AsyncStream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
AsyncStream::Tcp(stream) => Pin::new(stream).poll_read(cx, buf),
#[cfg(unix)]
AsyncStream::Unix(stream) => Pin::new(stream).poll_read(cx, buf),
#[cfg(windows)]
AsyncStream::NamedPipe(pipe) => Pin::new(pipe).poll_read(cx, buf),
}
}
}
impl AsyncWrite for AsyncStream {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
AsyncStream::Tcp(stream) => Pin::new(stream).poll_write(cx, buf),
#[cfg(unix)]
AsyncStream::Unix(stream) => Pin::new(stream).poll_write(cx, buf),
#[cfg(windows)]
AsyncStream::NamedPipe(pipe) => Pin::new(pipe).poll_write(cx, buf),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
AsyncStream::Tcp(stream) => Pin::new(stream).poll_flush(cx),
#[cfg(unix)]
AsyncStream::Unix(stream) => Pin::new(stream).poll_flush(cx),
#[cfg(windows)]
AsyncStream::NamedPipe(pipe) => Pin::new(pipe).poll_flush(cx),
}
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
AsyncStream::Tcp(stream) => Pin::new(stream).poll_shutdown(cx),
#[cfg(unix)]
AsyncStream::Unix(stream) => Pin::new(stream).poll_shutdown(cx),
#[cfg(windows)]
AsyncStream::NamedPipe(pipe) => Pin::new(pipe).poll_shutdown(cx),
}
}
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
AsyncStream::Tcp(stream) => Pin::new(stream).poll_write_vectored(cx, bufs),
#[cfg(unix)]
AsyncStream::Unix(stream) => Pin::new(stream).poll_write_vectored(cx, bufs),
#[cfg(windows)]
AsyncStream::NamedPipe(pipe) => Pin::new(pipe).poll_write_vectored(cx, bufs),
}
}
fn is_write_vectored(&self) -> bool {
match self {
AsyncStream::Tcp(stream) => stream.is_write_vectored(),
#[cfg(unix)]
AsyncStream::Unix(stream) => stream.is_write_vectored(),
#[cfg(windows)]
AsyncStream::NamedPipe(pipe) => pipe.is_write_vectored(),
}
}
}
#[cfg(test)]
mod tests {
#[expect(
clippy::assertions_on_constants,
reason = "compile-time invariant check kept as an assert for readability at the call site"
)]
#[test]
fn test_async_stream_variants_exist() {
assert!(true);
}
}