use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;
use tokio_rustls::client::TlsStream;
#[derive(Debug)]
pub enum Transport {
Plain(TcpStream),
Tls(Box<TlsStream<TcpStream>>),
}
impl Transport {
pub fn is_encrypted(&self) -> bool {
matches!(self, Transport::Tls(_))
}
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
match self {
Transport::Plain(s) => s.set_nodelay(nodelay),
Transport::Tls(s) => s.get_ref().0.set_nodelay(nodelay),
}
}
}
impl AsyncRead for Transport {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Transport::Plain(s) => Pin::new(s).poll_read(cx, buf),
Transport::Tls(s) => Pin::new(s.as_mut()).poll_read(cx, buf),
}
}
}
impl AsyncWrite for Transport {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
Transport::Plain(s) => Pin::new(s).poll_write(cx, buf),
Transport::Tls(s) => Pin::new(s.as_mut()).poll_write(cx, buf),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
Transport::Plain(s) => Pin::new(s).poll_flush(cx),
Transport::Tls(s) => Pin::new(s.as_mut()).poll_flush(cx),
}
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
Transport::Plain(s) => Pin::new(s).poll_shutdown(cx),
Transport::Tls(s) => Pin::new(s.as_mut()).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() {
Transport::Plain(s) => Pin::new(s).poll_write_vectored(cx, bufs),
Transport::Tls(s) => Pin::new(s.as_mut()).poll_write_vectored(cx, bufs),
}
}
fn is_write_vectored(&self) -> bool {
match self {
Transport::Plain(s) => s.is_write_vectored(),
Transport::Tls(s) => s.is_write_vectored(),
}
}
}