use std::io::{self, Read, Write};
#[cfg(feature = "tls")]
use crate::tls::TlsStream;
pub enum MaybeTls<S> {
Plain(S),
#[cfg(feature = "tls")]
Tls(Box<TlsStream<S>>),
}
impl<S> MaybeTls<S> {
pub fn is_tls(&self) -> bool {
#[cfg(feature = "tls")]
if matches!(self, Self::Tls(_)) {
return true;
}
false
}
}
impl<S: Read + Write> Read for MaybeTls<S> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self {
Self::Plain(s) => s.read(buf),
#[cfg(feature = "tls")]
Self::Tls(s) => s.read(buf),
}
}
}
impl<S: Read + Write> Write for MaybeTls<S> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self {
Self::Plain(s) => s.write(buf),
#[cfg(feature = "tls")]
Self::Tls(s) => s.write(buf),
}
}
fn flush(&mut self) -> io::Result<()> {
match self {
Self::Plain(s) => s.flush(),
#[cfg(feature = "tls")]
Self::Tls(s) => s.flush(),
}
}
}