kumo_tls_helper/
traits.rs1use std::fmt::Debug;
4use std::os::fd::{AsRawFd, FromRawFd};
5use tokio::io::{AsyncRead, AsyncWrite};
6use tokio::net::TcpStream;
7use tokio_openssl::SslStream;
8use tokio_rustls::client::TlsStream as TlsClientStream;
9use tokio_rustls::server::TlsStream as TlsServerStream;
10
11pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Debug + Unpin + Send + Sync {
12 fn try_dup(&self) -> Option<TcpStream> {
18 None
19 }
20
21 fn try_into_tcp_stream(self) -> Result<TcpStream, Self>
25 where
26 Self: Sized,
27 {
28 Err(self)
29 }
30}
31impl AsyncReadAndWrite for TlsClientStream<TcpStream> {}
32impl AsyncReadAndWrite for TlsClientStream<BoxedAsyncReadAndWrite> {}
33impl AsyncReadAndWrite for TlsServerStream<TcpStream> {}
34impl AsyncReadAndWrite for TlsServerStream<BoxedAsyncReadAndWrite> {}
35
36impl AsyncReadAndWrite for TcpStream {
37 fn try_dup(&self) -> Option<TcpStream> {
38 let fd = self.as_raw_fd();
39 let duplicate = unsafe { libc::dup(fd) };
42 if duplicate == -1 {
43 None
44 } else {
45 let duplicate_stream = unsafe { std::net::TcpStream::from_raw_fd(duplicate) };
49 TcpStream::from_std(duplicate_stream).ok()
50 }
51 }
52
53 fn try_into_tcp_stream(self) -> Result<TcpStream, Self> {
55 Ok(self)
56 }
57}
58impl AsyncReadAndWrite for SslStream<TcpStream> {}
59impl AsyncReadAndWrite for SslStream<BoxedAsyncReadAndWrite> {}
60impl AsyncReadAndWrite for tokio::net::UnixStream {}
61
62pub type BoxedAsyncReadAndWrite = Box<dyn AsyncReadAndWrite>;
63
64impl AsyncReadAndWrite for BoxedAsyncReadAndWrite {}