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;
pub enum Stream {
Plain(TcpStream),
Tls(TlsStream<TcpStream>),
}
impl Stream {
pub fn peer_addr(&self) -> io::Result<std::net::SocketAddr> {
match self {
Stream::Plain(s) => s.peer_addr(),
Stream::Tls(s) => s.get_ref().0.peer_addr(),
}
}
pub fn is_tls(&self) -> bool {
matches!(self, Stream::Tls(_))
}
}
impl AsyncRead for Stream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Stream::Plain(s) => Pin::new(s).poll_read(cx, buf),
Stream::Tls(s) => Pin::new(s).poll_read(cx, buf),
}
}
}
impl AsyncWrite for Stream {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
Stream::Plain(s) => Pin::new(s).poll_write(cx, buf),
Stream::Tls(s) => Pin::new(s).poll_write(cx, buf),
}
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Stream::Plain(s) => Pin::new(s).poll_flush(cx),
Stream::Tls(s) => Pin::new(s).poll_flush(cx),
}
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Stream::Plain(s) => Pin::new(s).poll_shutdown(cx),
Stream::Tls(s) => Pin::new(s).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() {
Stream::Plain(s) => Pin::new(s).poll_write_vectored(cx, bufs),
Stream::Tls(s) => Pin::new(s).poll_write_vectored(cx, bufs),
}
}
fn is_write_vectored(&self) -> bool {
match self {
Stream::Plain(s) => s.is_write_vectored(),
Stream::Tls(s) => s.is_write_vectored(),
}
}
}