From 430c06048db1ea8665a62311d4c9e0944651b46c Mon Sep 17 00:00:00 2001 From: Andreas Neuhaus Date: Fri, 10 Aug 2018 01:29:53 +0200 Subject: [PATCH] Add `MaybeTlsStream` wrapper type for streams that optionally use TLS Connecting to a URL wraps the connection into this wrapper type based on the protocol of the URL (`ws:` or `wss:`). Making this type public allows client applications to specify the websocket's type without importing the tokio_tls crate. --- src/connect.rs | 11 ++++++++--- src/lib.rs | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/connect.rs b/src/connect.rs index 7c4d02a..2573ab9 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -39,10 +39,12 @@ mod encryption { use tungstenite::Error; use tungstenite::stream::Mode; - use stream::NoDelay; + use stream::{NoDelay, Stream as StreamSwitcher}; - pub use stream::Stream as StreamSwitcher; - pub type AutoStream = StreamSwitcher>; + /// A stream that might be protected with TLS. + pub type MaybeTlsStream = StreamSwitcher>; + + pub type AutoStream = MaybeTlsStream; impl NoDelay for TlsStream { fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { @@ -68,6 +70,9 @@ mod encryption { } } +#[cfg(feature="tls")] +pub use self::encryption::MaybeTlsStream; + #[cfg(not(feature="tls"))] mod encryption { use futures::{future, Future}; diff --git a/src/lib.rs b/src/lib.rs index 05401de..7cfe2c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,9 @@ use tungstenite::{ #[cfg(feature="connect")] pub use connect::{connect_async, client_async_tls}; +#[cfg(all(feature="connect", feature="tls"))] +pub use connect::MaybeTlsStream; + /// Creates a WebSocket handshake from a request and a stream. /// For convenience, the user may call this with a url string, a URL, /// or a `Request`. Calling with `Request` allows the user to add