#![allow(clippy::large_enum_variant)]
#[cfg(feature = "tls")]
use rustls::ServerConnection;
use std::io::{Error, Read, Write};
use std::net::SocketAddr;
use std::time::Duration;
use std::net::TcpStream;
pub enum Stream {
Tcp(TcpStream),
#[cfg(feature = "tls")]
Tls(rustls::StreamOwned<ServerConnection, TcpStream>),
}
impl Read for Stream {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
Stream::Tcp(stream) => stream.read(buf),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.read(buf),
}
}
}
impl Write for Stream {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
Stream::Tcp(stream) => stream.write(buf),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.write(buf),
}
}
fn flush(&mut self) -> std::io::Result<()> {
match self {
Stream::Tcp(stream) => stream.flush(),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.flush(),
}
}
}
impl Stream {
pub fn peer_addr(&self) -> Result<SocketAddr, Error> {
match self {
Stream::Tcp(stream) => stream.peer_addr(),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.sock.peer_addr(),
}
}
pub fn shutdown(&self) -> std::io::Result<()> {
match self {
Stream::Tcp(stream) => stream.shutdown(std::net::Shutdown::Both),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.sock.shutdown(std::net::Shutdown::Both),
}
}
pub fn set_timeout(&self, timeout: Option<Duration>) -> std::io::Result<()> {
match self {
Stream::Tcp(stream) => {
stream.set_read_timeout(timeout)?;
stream.set_write_timeout(timeout)
}
#[cfg(feature = "tls")]
Stream::Tls(stream) => {
stream.sock.set_read_timeout(timeout)?;
stream.sock.set_write_timeout(timeout)
}
}
}
pub fn set_nonblocking(&self) -> std::io::Result<()> {
match self {
Stream::Tcp(stream) => stream.set_nonblocking(true),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.sock.set_nonblocking(true),
}
}
pub fn set_blocking(&self) -> std::io::Result<()> {
match self {
Stream::Tcp(stream) => stream.set_nonblocking(false),
#[cfg(feature = "tls")]
Stream::Tls(stream) => stream.sock.set_nonblocking(false),
}
}
}