use super::TlsStream;
use std::{
io,
pin::Pin,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
#[cfg_attr(feature = "__rustls", expect(clippy::large_enum_variant))]
#[derive(Debug)]
pub enum MaybeTlsStream<T> {
Tls(TlsStream<T>),
Plain(T),
}
impl<T: AsyncRead + AsyncWrite + Unpin> AsyncRead for MaybeTlsStream<T> {
#[inline]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Tls(stream) => Pin::new(stream).poll_read(cx, buf),
Self::Plain(stream) => Pin::new(stream).poll_read(cx, buf),
}
}
}
impl<T: AsyncRead + AsyncWrite + Unpin> AsyncWrite for MaybeTlsStream<T> {
#[inline]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
Self::Tls(stream) => Pin::new(stream).poll_write(cx, buf),
Self::Plain(stream) => Pin::new(stream).poll_write(cx, buf),
}
}
#[inline]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Tls(stream) => Pin::new(stream).poll_flush(cx),
Self::Plain(stream) => Pin::new(stream).poll_flush(cx),
}
}
#[inline]
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Tls(stream) => Pin::new(stream).poll_shutdown(cx),
Self::Plain(stream) => Pin::new(stream).poll_shutdown(cx),
}
}
}
#[cfg(feature = "__rustls")]
impl<S> From<tokio_rustls::server::TlsStream<S>> for MaybeTlsStream<S> {
fn from(stream: tokio_rustls::server::TlsStream<S>) -> Self {
Self::Tls(tokio_rustls::TlsStream::Server(stream))
}
}
#[cfg(feature = "__rustls")]
impl<S> From<tokio_rustls::client::TlsStream<S>> for MaybeTlsStream<S> {
fn from(stream: tokio_rustls::client::TlsStream<S>) -> Self {
Self::Tls(tokio_rustls::TlsStream::Client(stream))
}
}
#[cfg(feature = "nativetls")]
impl<S> From<tokio_native_tls::TlsStream<S>> for MaybeTlsStream<S> {
fn from(stream: tokio_native_tls::TlsStream<S>) -> Self {
Self::Tls(stream)
}
}