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;
use tokio_rustls::client::TlsStream;
pub enum PgStream {
Tcp(TcpStream),
Tls(Box<TlsStream<TcpStream>>),
#[cfg(all(target_os = "linux", feature = "io_uring"))]
Uring(super::uring::UringTcpStream),
#[cfg(unix)]
Unix(UnixStream),
#[cfg(all(feature = "enterprise-gssapi", target_os = "linux"))]
GssEnc(super::gss::GssEncStream),
}
impl AsyncRead for PgStream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
PgStream::Tcp(stream) => Pin::new(stream).poll_read(cx, buf),
PgStream::Tls(stream) => Pin::new(stream).poll_read(cx, buf),
#[cfg(all(target_os = "linux", feature = "io_uring"))]
PgStream::Uring(_) => Poll::Ready(Err(io::Error::new(
io::ErrorKind::Unsupported,
"io_uring stream read via AsyncRead is unsupported; use PgConnection helpers",
))),
#[cfg(unix)]
PgStream::Unix(stream) => Pin::new(stream).poll_read(cx, buf),
#[cfg(all(feature = "enterprise-gssapi", target_os = "linux"))]
PgStream::GssEnc(stream) => Pin::new(stream).poll_read(cx, buf),
}
}
}
impl AsyncWrite for PgStream {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
PgStream::Tcp(stream) => Pin::new(stream).poll_write(cx, buf),
PgStream::Tls(stream) => Pin::new(stream).poll_write(cx, buf),
#[cfg(all(target_os = "linux", feature = "io_uring"))]
PgStream::Uring(_) => Poll::Ready(Err(io::Error::new(
io::ErrorKind::Unsupported,
"io_uring stream write via AsyncWrite is unsupported; use PgConnection helpers",
))),
#[cfg(unix)]
PgStream::Unix(stream) => Pin::new(stream).poll_write(cx, buf),
#[cfg(all(feature = "enterprise-gssapi", target_os = "linux"))]
PgStream::GssEnc(stream) => Pin::new(stream).poll_write(cx, buf),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
PgStream::Tcp(stream) => Pin::new(stream).poll_flush(cx),
PgStream::Tls(stream) => Pin::new(stream).poll_flush(cx),
#[cfg(all(target_os = "linux", feature = "io_uring"))]
PgStream::Uring(_) => Poll::Ready(Ok(())),
#[cfg(unix)]
PgStream::Unix(stream) => Pin::new(stream).poll_flush(cx),
#[cfg(all(feature = "enterprise-gssapi", target_os = "linux"))]
PgStream::GssEnc(stream) => Pin::new(stream).poll_flush(cx),
}
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
PgStream::Tcp(stream) => Pin::new(stream).poll_shutdown(cx),
PgStream::Tls(stream) => Pin::new(stream).poll_shutdown(cx),
#[cfg(all(target_os = "linux", feature = "io_uring"))]
PgStream::Uring(_) => Poll::Ready(Ok(())),
#[cfg(unix)]
PgStream::Unix(stream) => Pin::new(stream).poll_shutdown(cx),
#[cfg(all(feature = "enterprise-gssapi", target_os = "linux"))]
PgStream::GssEnc(stream) => Pin::new(stream).poll_shutdown(cx),
}
}
}