use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;
use tokio_rustls::server::TlsStream;
pub enum ClientStream {
Tcp(TcpStream),
Tls(Box<TlsStream<TcpStream>>),
}
impl AsyncRead for ClientStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
match &mut *self {
ClientStream::Tcp(stream) => Pin::new(stream).poll_read(cx, buf),
ClientStream::Tls(stream) => Pin::new(stream.as_mut()).poll_read(cx, buf),
}
}
}
impl AsyncWrite for ClientStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
match &mut *self {
ClientStream::Tcp(stream) => Pin::new(stream).poll_write(cx, buf),
ClientStream::Tls(stream) => Pin::new(stream.as_mut()).poll_write(cx, buf),
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
match &mut *self {
ClientStream::Tcp(stream) => Pin::new(stream).poll_flush(cx),
ClientStream::Tls(stream) => Pin::new(stream.as_mut()).poll_flush(cx),
}
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
match &mut *self {
ClientStream::Tcp(stream) => Pin::new(stream).poll_shutdown(cx),
ClientStream::Tls(stream) => Pin::new(stream.as_mut()).poll_shutdown(cx),
}
}
}