tls_api_rustls/
stream.rs

1use std::fmt;
2use std::fmt::Debug;
3use std::marker::PhantomData;
4
5use tls_api::async_as_sync::AsyncIoAsSyncIo;
6use tls_api::async_as_sync::AsyncWrapperOps;
7use tls_api::async_as_sync::TlsStreamOverSyncIo;
8use tls_api::spi_async_socket_impl_delegate;
9use tls_api::spi_tls_stream_over_sync_io_wrapper;
10use tls_api::AsyncSocket;
11use tls_api::ImplInfo;
12
13use crate::rustls_utils::RustlsStream;
14use crate::RustlsSessionRef;
15
16spi_tls_stream_over_sync_io_wrapper!(TlsStream, RustlsStream);
17
18impl<A: AsyncSocket> TlsStream<A> {
19    /// Get the `rustls` session.
20    pub fn session(&self) -> RustlsSessionRef {
21        self.0.stream.session()
22    }
23}
24
25#[derive(Debug)]
26pub(crate) struct AsyncWrapperOpsImpl<S, A>(PhantomData<(S, A)>)
27where
28    S: fmt::Debug + Unpin + Send + 'static,
29    A: AsyncSocket;
30
31#[derive(Debug)]
32struct StreamOwnedDebug;
33
34impl<S, A> AsyncWrapperOps<A> for AsyncWrapperOpsImpl<S, A>
35where
36    S: fmt::Debug + Unpin + Send + 'static,
37    A: AsyncSocket,
38{
39    type SyncWrapper = RustlsStream<AsyncIoAsSyncIo<A>>;
40
41    fn impl_info() -> ImplInfo {
42        crate::info()
43    }
44
45    fn debug(_w: &Self::SyncWrapper) -> &dyn Debug {
46        // TODO: remove on next release https://github.com/ctz/rustls/pull/524
47        &StreamOwnedDebug
48    }
49
50    fn get_mut(w: &mut Self::SyncWrapper) -> &mut AsyncIoAsSyncIo<A> {
51        w.get_socket_mut()
52    }
53
54    fn get_ref(w: &Self::SyncWrapper) -> &AsyncIoAsSyncIo<A> {
55        w.get_socket_ref()
56    }
57
58    fn get_alpn_protocol(w: &Self::SyncWrapper) -> anyhow::Result<Option<Vec<u8>>> {
59        Ok(w.get_alpn_protocol().map(Vec::from))
60    }
61}