-
Notifications
You must be signed in to change notification settings - Fork 121
feat(tls,ws): move MaybeTlsStream to tls #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use std::io; | ||
|
|
||
| use compio_buf::{BufResult, IoBuf, IoBufMut}; | ||
| use compio_io::{AsyncRead, AsyncWrite}; | ||
|
|
||
| use crate::TlsStream; | ||
|
|
||
| /// Stream that can be either plain TCP or TLS-encrypted | ||
| #[derive(Debug)] | ||
| #[allow(clippy::large_enum_variant)] | ||
| pub enum MaybeTlsStream<S> { | ||
| /// Plain, unencrypted stream | ||
| Plain(S), | ||
| /// TLS-encrypted stream | ||
| Tls(TlsStream<S>), | ||
| } | ||
|
|
||
| impl<S> MaybeTlsStream<S> { | ||
| /// Create an unencrypted stream. | ||
| pub fn plain(stream: S) -> Self { | ||
| MaybeTlsStream::Plain(stream) | ||
| } | ||
|
|
||
| /// Create a TLS-encrypted stream. | ||
| pub fn tls(stream: TlsStream<S>) -> Self { | ||
| MaybeTlsStream::Tls(stream) | ||
| } | ||
|
|
||
| /// Whether the stream is TLS-encrypted. | ||
| pub fn is_tls(&self) -> bool { | ||
| matches!(self, MaybeTlsStream::Tls(_)) | ||
| } | ||
| } | ||
|
|
||
| impl<S> AsyncRead for MaybeTlsStream<S> | ||
| where | ||
| S: AsyncRead + AsyncWrite + Unpin + 'static, | ||
| { | ||
| async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.read(buf).await, | ||
| MaybeTlsStream::Tls(stream) => stream.read(buf).await, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S> AsyncWrite for MaybeTlsStream<S> | ||
| where | ||
| S: AsyncRead + AsyncWrite + Unpin + 'static, | ||
| { | ||
| async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.write(buf).await, | ||
| MaybeTlsStream::Tls(stream) => stream.write(buf).await, | ||
| } | ||
| } | ||
|
|
||
| async fn flush(&mut self) -> io::Result<()> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.flush().await, | ||
| MaybeTlsStream::Tls(stream) => stream.flush().await, | ||
| } | ||
| } | ||
|
|
||
| async fn shutdown(&mut self) -> io::Result<()> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.shutdown().await, | ||
| MaybeTlsStream::Tls(stream) => stream.shutdown().await, | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,93 +1,4 @@ | ||
| //! Provides [`MaybeTlsStream`]. | ||
|
|
||
| #[cfg(feature = "rustls")] | ||
| use std::io::Result as IoResult; | ||
|
|
||
| #[cfg(feature = "rustls")] | ||
| use compio_buf::{BufResult, IoBuf, IoBufMut}; | ||
| #[cfg(feature = "rustls")] | ||
| use compio_io::{AsyncRead, AsyncWrite}; | ||
| #[cfg(feature = "rustls")] | ||
| use compio_tls::TlsStream; | ||
|
|
||
| /// Stream that can be either plain TCP or TLS-encrypted | ||
| #[cfg(feature = "rustls")] | ||
| #[derive(Debug)] | ||
| #[allow(clippy::large_enum_variant)] | ||
| pub enum MaybeTlsStream<S> { | ||
| /// Plain, unencrypted stream | ||
| Plain(S), | ||
| /// TLS-encrypted stream | ||
| #[cfg(feature = "rustls")] | ||
| Tls(TlsStream<S>), | ||
| } | ||
|
|
||
| #[cfg(feature = "rustls")] | ||
| impl<S> MaybeTlsStream<S> { | ||
| /// Create an unencrypted stream. | ||
| pub fn plain(stream: S) -> Self { | ||
| MaybeTlsStream::Plain(stream) | ||
| } | ||
|
|
||
| /// Create a TLS-encrypted stream. | ||
| #[cfg(feature = "rustls")] | ||
| pub fn tls(stream: TlsStream<S>) -> Self { | ||
| MaybeTlsStream::Tls(stream) | ||
| } | ||
|
|
||
| /// Whether the stream is TLS-encrypted. | ||
| pub fn is_tls(&self) -> bool { | ||
| #[cfg(feature = "rustls")] | ||
| { | ||
| matches!(self, MaybeTlsStream::Tls(_)) | ||
| } | ||
| #[cfg(not(feature = "rustls"))] | ||
| { | ||
| false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "rustls")] | ||
| impl<S> AsyncRead for MaybeTlsStream<S> | ||
| where | ||
| S: AsyncRead + AsyncWrite + Unpin + 'static, | ||
| { | ||
| async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.read(buf).await, | ||
| #[cfg(feature = "rustls")] | ||
| MaybeTlsStream::Tls(stream) => stream.read(buf).await, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "rustls")] | ||
| impl<S> AsyncWrite for MaybeTlsStream<S> | ||
| where | ||
| S: AsyncRead + AsyncWrite + Unpin + 'static, | ||
| { | ||
| async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.write(buf).await, | ||
| #[cfg(feature = "rustls")] | ||
| MaybeTlsStream::Tls(stream) => stream.write(buf).await, | ||
| } | ||
| } | ||
|
|
||
| async fn flush(&mut self) -> IoResult<()> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.flush().await, | ||
| #[cfg(feature = "rustls")] | ||
| MaybeTlsStream::Tls(stream) => stream.flush().await, | ||
| } | ||
| } | ||
|
|
||
| async fn shutdown(&mut self) -> IoResult<()> { | ||
| match self { | ||
| MaybeTlsStream::Plain(stream) => stream.shutdown().await, | ||
| #[cfg(feature = "rustls")] | ||
| MaybeTlsStream::Tls(stream) => stream.shutdown().await, | ||
| } | ||
| } | ||
| } | ||
| pub use compio_tls::MaybeTlsStream; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.