kumo_tls_helper/
traits.rs

1//! Async stream traits for TLS and plain TCP connections.
2
3use std::fmt::Debug;
4use tokio::io::{AsyncRead, AsyncWrite};
5use tokio::net::TcpStream;
6use tokio_openssl::SslStream;
7use tokio_rustls::client::TlsStream as TlsClientStream;
8use tokio_rustls::server::TlsStream as TlsServerStream;
9
10pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Debug + Unpin + Send + Sync {
11    /// Returns Ok() if the type can be converted without loss to a TcpStream,
12    /// or Err(self) otherwise.  This is used by the proxy server to decide
13    /// whether we can use splice(2).
14    fn try_into_tcp_stream(self) -> Result<TcpStream, Self>
15    where
16        Self: Sized,
17    {
18        Err(self)
19    }
20}
21impl AsyncReadAndWrite for TlsClientStream<TcpStream> {}
22impl AsyncReadAndWrite for TlsClientStream<BoxedAsyncReadAndWrite> {}
23impl AsyncReadAndWrite for TlsServerStream<TcpStream> {}
24impl AsyncReadAndWrite for TlsServerStream<BoxedAsyncReadAndWrite> {}
25
26impl AsyncReadAndWrite for TcpStream {
27    // Yes, a TcpStream can be converted to a TcpStream
28    fn try_into_tcp_stream(self) -> Result<TcpStream, Self> {
29        Ok(self)
30    }
31}
32impl AsyncReadAndWrite for SslStream<TcpStream> {}
33impl AsyncReadAndWrite for SslStream<BoxedAsyncReadAndWrite> {}
34impl AsyncReadAndWrite for tokio::net::UnixStream {}
35
36pub type BoxedAsyncReadAndWrite = Box<dyn AsyncReadAndWrite>;
37
38impl AsyncReadAndWrite for BoxedAsyncReadAndWrite {}