|
1 | 1 | //! Provides [`MaybeTlsStream`]. |
2 | 2 |
|
3 | 3 | #[cfg(feature = "rustls")] |
4 | | -use std::io::Result as IoResult; |
5 | | - |
6 | | -#[cfg(feature = "rustls")] |
7 | | -use compio_buf::{BufResult, IoBuf, IoBufMut}; |
8 | | -#[cfg(feature = "rustls")] |
9 | | -use compio_io::{AsyncRead, AsyncWrite}; |
10 | | -#[cfg(feature = "rustls")] |
11 | | -use compio_tls::TlsStream; |
12 | | - |
13 | | -/// Stream that can be either plain TCP or TLS-encrypted |
14 | | -#[cfg(feature = "rustls")] |
15 | | -#[derive(Debug)] |
16 | | -#[allow(clippy::large_enum_variant)] |
17 | | -pub enum MaybeTlsStream<S> { |
18 | | - /// Plain, unencrypted stream |
19 | | - Plain(S), |
20 | | - /// TLS-encrypted stream |
21 | | - #[cfg(feature = "rustls")] |
22 | | - Tls(TlsStream<S>), |
23 | | -} |
24 | | - |
25 | | -#[cfg(feature = "rustls")] |
26 | | -impl<S> MaybeTlsStream<S> { |
27 | | - /// Create an unencrypted stream. |
28 | | - pub fn plain(stream: S) -> Self { |
29 | | - MaybeTlsStream::Plain(stream) |
30 | | - } |
31 | | - |
32 | | - /// Create a TLS-encrypted stream. |
33 | | - #[cfg(feature = "rustls")] |
34 | | - pub fn tls(stream: TlsStream<S>) -> Self { |
35 | | - MaybeTlsStream::Tls(stream) |
36 | | - } |
37 | | - |
38 | | - /// Whether the stream is TLS-encrypted. |
39 | | - pub fn is_tls(&self) -> bool { |
40 | | - #[cfg(feature = "rustls")] |
41 | | - { |
42 | | - matches!(self, MaybeTlsStream::Tls(_)) |
43 | | - } |
44 | | - #[cfg(not(feature = "rustls"))] |
45 | | - { |
46 | | - false |
47 | | - } |
48 | | - } |
49 | | -} |
50 | | - |
51 | | -#[cfg(feature = "rustls")] |
52 | | -impl<S> AsyncRead for MaybeTlsStream<S> |
53 | | -where |
54 | | - S: AsyncRead + AsyncWrite + Unpin + 'static, |
55 | | -{ |
56 | | - async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> { |
57 | | - match self { |
58 | | - MaybeTlsStream::Plain(stream) => stream.read(buf).await, |
59 | | - #[cfg(feature = "rustls")] |
60 | | - MaybeTlsStream::Tls(stream) => stream.read(buf).await, |
61 | | - } |
62 | | - } |
63 | | -} |
64 | | - |
65 | | -#[cfg(feature = "rustls")] |
66 | | -impl<S> AsyncWrite for MaybeTlsStream<S> |
67 | | -where |
68 | | - S: AsyncRead + AsyncWrite + Unpin + 'static, |
69 | | -{ |
70 | | - async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> { |
71 | | - match self { |
72 | | - MaybeTlsStream::Plain(stream) => stream.write(buf).await, |
73 | | - #[cfg(feature = "rustls")] |
74 | | - MaybeTlsStream::Tls(stream) => stream.write(buf).await, |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - async fn flush(&mut self) -> IoResult<()> { |
79 | | - match self { |
80 | | - MaybeTlsStream::Plain(stream) => stream.flush().await, |
81 | | - #[cfg(feature = "rustls")] |
82 | | - MaybeTlsStream::Tls(stream) => stream.flush().await, |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - async fn shutdown(&mut self) -> IoResult<()> { |
87 | | - match self { |
88 | | - MaybeTlsStream::Plain(stream) => stream.shutdown().await, |
89 | | - #[cfg(feature = "rustls")] |
90 | | - MaybeTlsStream::Tls(stream) => stream.shutdown().await, |
91 | | - } |
92 | | - } |
93 | | -} |
| 4 | +pub use compio_tls::MaybeTlsStream; |
0 commit comments