Skip to content

Commit 6e760ca

Browse files
authored
feat(tls,ws): move MaybeTlsStream to tls (#516)
* feat(tls,ws): move MaybeTlsStream to tls * feat(tls,ws): bump versions * fix(tls): remove feature gates * fix(ws): move deprecated attr
1 parent 3956a2c commit 6e760ca

8 files changed

Lines changed: 80 additions & 97 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ compio-net = { path = "./compio-net", version = "0.9.0" }
3636
compio-signal = { path = "./compio-signal", version = "0.7.0" }
3737
compio-dispatcher = { path = "./compio-dispatcher", version = "0.8.1" }
3838
compio-log = { path = "./compio-log", version = "0.1.0" }
39-
compio-tls = { path = "./compio-tls", version = "0.7.1", default-features = false }
39+
compio-tls = { path = "./compio-tls", version = "0.7.2", default-features = false }
4040
compio-process = { path = "./compio-process", version = "0.6.0" }
4141
compio-quic = { path = "./compio-quic", version = "0.5.1", default-features = false }
4242
compio-ws = { path = "./compio-ws", version = "0.1.0", default-features = false }

compio-tls/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "compio-tls"
3-
version = "0.7.1"
3+
version = "0.7.2"
44
description = "TLS adaptor with compio"
55
categories = ["asynchronous", "network-programming"]
66
keywords = ["async", "net", "tls"]

compio-tls/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub use native_tls;
1313
pub use rustls;
1414

1515
mod adapter;
16+
mod maybe;
1617
mod stream;
1718

1819
pub use adapter::*;
20+
pub use maybe::*;
1921
pub use stream::*;

compio-tls/src/maybe.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::io;
2+
3+
use compio_buf::{BufResult, IoBuf, IoBufMut};
4+
use compio_io::{AsyncRead, AsyncWrite};
5+
6+
use crate::TlsStream;
7+
8+
/// Stream that can be either plain TCP or TLS-encrypted
9+
#[derive(Debug)]
10+
#[allow(clippy::large_enum_variant)]
11+
pub enum MaybeTlsStream<S> {
12+
/// Plain, unencrypted stream
13+
Plain(S),
14+
/// TLS-encrypted stream
15+
Tls(TlsStream<S>),
16+
}
17+
18+
impl<S> MaybeTlsStream<S> {
19+
/// Create an unencrypted stream.
20+
pub fn plain(stream: S) -> Self {
21+
MaybeTlsStream::Plain(stream)
22+
}
23+
24+
/// Create a TLS-encrypted stream.
25+
pub fn tls(stream: TlsStream<S>) -> Self {
26+
MaybeTlsStream::Tls(stream)
27+
}
28+
29+
/// Whether the stream is TLS-encrypted.
30+
pub fn is_tls(&self) -> bool {
31+
matches!(self, MaybeTlsStream::Tls(_))
32+
}
33+
}
34+
35+
impl<S> AsyncRead for MaybeTlsStream<S>
36+
where
37+
S: AsyncRead + AsyncWrite + Unpin + 'static,
38+
{
39+
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
40+
match self {
41+
MaybeTlsStream::Plain(stream) => stream.read(buf).await,
42+
MaybeTlsStream::Tls(stream) => stream.read(buf).await,
43+
}
44+
}
45+
}
46+
47+
impl<S> AsyncWrite for MaybeTlsStream<S>
48+
where
49+
S: AsyncRead + AsyncWrite + Unpin + 'static,
50+
{
51+
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
52+
match self {
53+
MaybeTlsStream::Plain(stream) => stream.write(buf).await,
54+
MaybeTlsStream::Tls(stream) => stream.write(buf).await,
55+
}
56+
}
57+
58+
async fn flush(&mut self) -> io::Result<()> {
59+
match self {
60+
MaybeTlsStream::Plain(stream) => stream.flush().await,
61+
MaybeTlsStream::Tls(stream) => stream.flush().await,
62+
}
63+
}
64+
65+
async fn shutdown(&mut self) -> io::Result<()> {
66+
match self {
67+
MaybeTlsStream::Plain(stream) => stream.shutdown().await,
68+
MaybeTlsStream::Tls(stream) => stream.shutdown().await,
69+
}
70+
}
71+
}

compio-ws/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "compio-ws"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "WebSocket library for the compio runtime"
55
edition = { workspace = true }
66
authors = { workspace = true }

compio-ws/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![cfg_attr(docsrs, feature(doc_cfg))]
1111
#![warn(missing_docs)]
1212

13+
#[deprecated = "Use `compio-tls` crate instead."]
1314
pub mod stream;
1415

1516
#[cfg(feature = "rustls")]

compio-ws/src/rustls.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55

66
use compio_io::{AsyncRead, AsyncWrite};
77
use compio_net::TcpStream;
8-
use compio_tls::TlsConnector;
8+
use compio_tls::{MaybeTlsStream, TlsConnector};
99
#[cfg(any(feature = "rustls-platform-verifier", feature = "webpki-roots"))]
1010
use rustls::{ClientConfig, RootCertStore};
1111
use tungstenite::{
@@ -15,9 +15,7 @@ use tungstenite::{
1515
stream::Mode,
1616
};
1717

18-
use crate::{
19-
WebSocketConfig, WebSocketStream, client_async_with_config, domain, stream::MaybeTlsStream,
20-
};
18+
use crate::{WebSocketConfig, WebSocketStream, client_async_with_config, domain};
2119

2220
/// Type alias for a stream that can be either plain TCP or TLS-encrypted.
2321
pub type AutoStream<S> = MaybeTlsStream<S>;

compio-ws/src/stream.rs

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,4 @@
11
//! Provides [`MaybeTlsStream`].
22
33
#[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

Comments
 (0)