nusadb 0.1.0

Fast, stable native Rust driver and ORM for NusaDB (Nusa Wire Protocol 1.1).
Documentation
//! The sync byte transport under a [`Connection`](crate::Connection): a plaintext TCP socket or, with
//! the `tls` feature, a rustls TLS 1.3 stream. Both implement `Read + Write`, so the framing codec is
//! oblivious to which is in use.

use std::io::{Read, Result as IoResult, Write};
use std::net::TcpStream;

/// A connection's underlying byte stream.
pub(crate) enum Transport {
    /// Plaintext TCP.
    Plain(TcpStream),
    /// TLS 1.3 over TCP (feature `tls`).
    #[cfg(feature = "tls")]
    Tls(Box<rustls::StreamOwned<rustls::ClientConnection, TcpStream>>),
}

impl Transport {
    /// The underlying TCP socket — used to set a read timeout for the notification poll path
    /// (`poll_notification`). For TLS it is the socket beneath the rustls stream.
    pub(crate) fn tcp(&self) -> &TcpStream {
        match self {
            Self::Plain(s) => s,
            #[cfg(feature = "tls")]
            Self::Tls(s) => &s.sock,
        }
    }
}

impl Read for Transport {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
        match self {
            Self::Plain(s) => s.read(buf),
            #[cfg(feature = "tls")]
            Self::Tls(s) => s.read(buf),
        }
    }
}

impl Write for Transport {
    fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
        match self {
            Self::Plain(s) => s.write(buf),
            #[cfg(feature = "tls")]
            Self::Tls(s) => s.write(buf),
        }
    }

    fn flush(&mut self) -> IoResult<()> {
        match self {
            Self::Plain(s) => s.flush(),
            #[cfg(feature = "tls")]
            Self::Tls(s) => s.flush(),
        }
    }
}