tls_api_not_tls/
stream.rs

1use std::io;
2use std::pin::Pin;
3use std::task::Context;
4use std::task::Poll;
5use tls_api::runtime::AsyncRead;
6use tls_api::runtime::AsyncWrite;
7use tls_api::spi::TlsStreamWithUpcastDyn;
8use tls_api::AsyncSocket;
9use tls_api::ImplInfo;
10use tls_api::TlsStreamDyn;
11use tls_api::TlsStreamWithSocketDyn;
12
13#[derive(Debug)]
14pub struct TlsStream<A>(pub A)
15where
16    A: AsyncSocket;
17
18impl<A: AsyncSocket> TlsStream<A> {
19    fn get_inner(self: Pin<&mut Self>) -> Pin<&mut A> {
20        Pin::new(&mut self.get_mut().0)
21    }
22}
23
24impl<A: AsyncSocket> TlsStreamDyn for TlsStream<A> {
25    fn impl_info(&self) -> ImplInfo {
26        crate::info()
27    }
28
29    fn get_alpn_protocol(&self) -> anyhow::Result<Option<Vec<u8>>> {
30        Err(crate::Error::Alpn.into())
31    }
32
33    fn get_socket_dyn_mut(&mut self) -> &mut dyn AsyncSocket {
34        &mut self.0
35    }
36
37    fn get_socket_dyn_ref(&self) -> &dyn AsyncSocket {
38        &self.0
39    }
40}
41
42impl<A: AsyncSocket> TlsStreamWithSocketDyn<A> for TlsStream<A> {
43    fn get_socket_mut(&mut self) -> &mut A {
44        &mut self.0
45    }
46
47    fn get_socket_ref(&self) -> &A {
48        &self.0
49    }
50}
51
52impl<A: AsyncSocket> TlsStreamWithUpcastDyn<A> for TlsStream<A> {
53    fn upcast_box(self: Box<Self>) -> Box<dyn TlsStreamDyn> {
54        self
55    }
56}
57
58impl<A: AsyncSocket> AsyncRead for TlsStream<A> {
59    #[cfg(feature = "runtime-tokio")]
60    fn poll_read(
61        self: Pin<&mut Self>,
62        cx: &mut Context<'_>,
63        buf: &mut tokio::io::ReadBuf,
64    ) -> Poll<io::Result<()>> {
65        self.get_inner().poll_read(cx, buf)
66    }
67
68    #[cfg(feature = "runtime-async-std")]
69    fn poll_read(
70        self: Pin<&mut Self>,
71        cx: &mut Context<'_>,
72        buf: &mut [u8],
73    ) -> Poll<io::Result<usize>> {
74        self.get_inner().poll_read(cx, buf)
75    }
76}
77
78impl<A: AsyncSocket> AsyncWrite for TlsStream<A> {
79    fn poll_write(
80        self: Pin<&mut Self>,
81        cx: &mut Context<'_>,
82        buf: &[u8],
83    ) -> Poll<io::Result<usize>> {
84        self.get_inner().poll_write(cx, buf)
85    }
86
87    #[cfg(feature = "runtime-async-std")]
88    fn poll_write_vectored(
89        self: Pin<&mut Self>,
90        cx: &mut Context<'_>,
91        bufs: &[std::io::IoSlice<'_>],
92    ) -> Poll<io::Result<usize>> {
93        self.get_inner().poll_write_vectored(cx, bufs)
94    }
95
96    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
97        self.get_inner().poll_flush(cx)
98    }
99
100    #[cfg(feature = "runtime-tokio")]
101    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
102        self.get_inner().poll_shutdown(cx)
103    }
104
105    #[cfg(feature = "runtime-async-std")]
106    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
107        self.get_inner().poll_close(cx)
108    }
109}