tls_api_native_tls/
stream.rs1use std::fmt;
2use std::io;
3use std::io::Read;
4use std::io::Write;
5use std::marker::PhantomData;
6use tls_api::async_as_sync::AsyncIoAsSyncIo;
7use tls_api::async_as_sync::AsyncWrapperOps;
8use tls_api::async_as_sync::TlsStreamOverSyncIo;
9use tls_api::async_as_sync::WriteShutdown;
10use tls_api::spi_async_socket_impl_delegate;
11use tls_api::spi_tls_stream_over_sync_io_wrapper;
12use tls_api::AsyncSocket;
13use tls_api::ImplInfo;
14
15spi_tls_stream_over_sync_io_wrapper!(TlsStream, NativeTlsStream);
16
17#[derive(Debug)]
18pub(crate) struct AsyncWrapperOpsImpl<S, A>(PhantomData<(S, A)>)
19where
20 S: fmt::Debug + Unpin + Send + 'static,
21 A: AsyncSocket;
22
23impl<S, A> AsyncWrapperOps<A> for AsyncWrapperOpsImpl<S, A>
24where
25 S: fmt::Debug + Unpin + Send + 'static,
26 A: AsyncSocket,
27{
28 type SyncWrapper = NativeTlsStream<AsyncIoAsSyncIo<A>>;
29
30 fn impl_info() -> ImplInfo {
31 crate::info()
32 }
33
34 fn debug(w: &Self::SyncWrapper) -> &dyn fmt::Debug {
35 &w.0
36 }
37
38 fn get_mut(w: &mut Self::SyncWrapper) -> &mut AsyncIoAsSyncIo<A> {
39 w.0.get_mut()
40 }
41
42 fn get_ref(w: &Self::SyncWrapper) -> &AsyncIoAsSyncIo<A> {
43 w.0.get_ref()
44 }
45
46 fn get_alpn_protocol(w: &Self::SyncWrapper) -> anyhow::Result<Option<Vec<u8>>> {
47 w.0.negotiated_alpn().map_err(anyhow::Error::new)
48 }
49}
50
51pub(crate) struct NativeTlsStream<A: Read + Write>(pub(crate) native_tls::TlsStream<A>);
52
53impl<A: Read + Write> Write for NativeTlsStream<A> {
54 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
55 self.0.write(buf)
56 }
57
58 fn flush(&mut self) -> io::Result<()> {
59 self.0.flush()
60 }
61
62 fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
63 self.0.write_vectored(bufs)
64 }
65
66 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
67 self.0.write_all(buf)
68 }
69
70 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
71 self.0.write_fmt(fmt)
72 }
73}
74
75impl<A: Read + Write> WriteShutdown for NativeTlsStream<A> {
76 fn shutdown(&mut self) -> Result<(), io::Error> {
77 self.flush()?;
78 self.0.shutdown()?;
79 Ok(())
80 }
81}
82
83impl<A: Read + Write> Read for NativeTlsStream<A> {
84 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
85 self.0.read(buf)
86 }
87
88 fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
89 self.0.read_vectored(bufs)
90 }
91
92 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
93 self.0.read_to_end(buf)
94 }
95
96 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
97 self.0.read_to_string(buf)
98 }
99
100 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
101 self.0.read_exact(buf)
102 }
103}