qail-pg 0.27.0

Fastest async PostgreSQL driver - AST to wire protocol, optional io_uring on Linux
Documentation
//! Stream abstraction for TCP, TLS, and Unix socket connections.
//!
//! This module provides a unified interface for all connection types.

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;

/// A PostgreSQL connection stream (TCP, TLS, Unix, or GSSENC).
pub enum PgStream {
    Tcp(TcpStream),
    Tls(Box<TlsStream<TcpStream>>),
    /// Linux io_uring plain TCP transport.
    #[cfg(all(target_os = "linux", feature = "io_uring"))]
    Uring(super::uring::UringTcpStream),
    /// Unix domain socket connection
    #[cfg(unix)]
    Unix(UnixStream),
    /// GSSAPI session-encrypted connection
    #[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),
        }
    }
}