Skip to content

Commit 5238b56

Browse files
committed
Restructured to make crate prefer neither sync or async.
1 parent 117c3f3 commit 5238b56

File tree

18 files changed

+1005
-911
lines changed

18 files changed

+1005
-911
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ native-tls = { version = "^0.1.2", optional = true }
3636
git = "https://github.com/tokio-rs/tokio-io.git"
3737

3838
[features]
39-
default = ["ssl", "async", "async-ssl"]
40-
ssl = ["native-tls"]
41-
nightly = ["hyper/nightly"]
39+
default = ["sync", "sync-ssl", "async", "async-ssl"]
40+
sync = []
41+
sync-ssl = ["native-tls", "sync"]
4242
async = ["tokio-core", "tokio-io", "bytes", "futures"]
4343
async-ssl = ["native-tls", "tokio-tls", "async"]
44+
nightly = ["hyper/nightly"]

src/client/async.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub use tokio_core::reactor::Handle;
2+
pub use tokio_io::codec::Framed;
3+
pub use tokio_core::net::TcpStream;
4+
pub use futures::Future;
5+
use hyper::header::Headers;
6+
7+
use result::WebSocketError;
8+
use codec::ws::MessageCodec;
9+
use message::OwnedMessage;
10+
11+
#[cfg(feature="async-ssl")]
12+
pub use tokio_tls::TlsStream;
13+
14+
pub type Client<S> = Framed<S, MessageCodec<OwnedMessage>>;
15+
16+
pub type ClientNew<S> = Box<Future<Item = (Client<S>, Headers), Error = WebSocketError>>;
17+

src/client/builder.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ use hyper::http::h1::parse_response;
1616
use hyper::header::{Headers, Header, HeaderFormat, Host, Connection, ConnectionOption, Upgrade,
1717
Protocol, ProtocolName};
1818
use unicase::UniCase;
19-
#[cfg(any(feature="ssl", feature="async-ssl"))]
20-
use native_tls::{TlsStream, TlsConnector};
2119
use header::extensions::Extension;
2220
use header::{WebSocketAccept, WebSocketKey, WebSocketVersion, WebSocketProtocol,
2321
WebSocketExtensions, Origin};
2422
use result::{WSUrlErrorKind, WebSocketResult, WebSocketError};
25-
#[cfg(feature="ssl")]
26-
use stream::NetworkStream;
2723
use stream::{self, Stream};
2824

29-
use super::Client;
25+
#[cfg(feature="sync")]
26+
use super::sync::Client;
27+
28+
#[cfg(feature="sync-ssl")]
29+
use stream::sync::NetworkStream;
30+
31+
#[cfg(any(feature="sync-ssl", feature="async-ssl"))]
32+
use native_tls::{TlsStream, TlsConnector};
3033

3134
#[cfg(feature="async")]
3235
mod async_imports {
@@ -375,7 +378,7 @@ impl<'u> ClientBuilder<'u> {
375378
/// let message = Message::text("m337 47 7pm");
376379
/// client.send_message(&message).unwrap();
377380
/// ```
378-
#[cfg(feature="ssl")]
381+
#[cfg(feature="sync-ssl")]
379382
pub fn connect(
380383
&mut self,
381384
ssl_config: Option<TlsConnector>,
@@ -406,6 +409,7 @@ impl<'u> ClientBuilder<'u> {
406409
/// // split into two (for some reason)!
407410
/// let (receiver, sender) = client.split().unwrap();
408411
/// ```
412+
#[cfg(feature="sync")]
409413
pub fn connect_insecure(&mut self) -> WebSocketResult<Client<TcpStream>> {
410414
let tcp_stream = try!(self.establish_tcp(Some(false)));
411415

@@ -416,7 +420,7 @@ impl<'u> ClientBuilder<'u> {
416420
/// This will only use an `SslStream`, this is useful
417421
/// when you want to be sure to connect over SSL or when you want access
418422
/// to the `SslStream` functions (without having to go through a `Box`).
419-
#[cfg(feature="ssl")]
423+
#[cfg(feature="sync-ssl")]
420424
pub fn connect_secure(
421425
&mut self,
422426
ssl_config: Option<TlsConnector>,
@@ -458,6 +462,7 @@ impl<'u> ClientBuilder<'u> {
458462
/// let text = String::from_utf8(text).unwrap();
459463
/// assert!(text.contains("dGhlIHNhbXBsZSBub25jZQ=="), "{}", text);
460464
/// ```
465+
#[cfg(feature="sync")]
461466
pub fn connect_on<S>(&mut self, mut stream: S) -> WebSocketResult<Client<S>>
462467
where S: Stream
463468
{
@@ -476,6 +481,15 @@ impl<'u> ClientBuilder<'u> {
476481
Ok(Client::unchecked(reader, response.headers, true, false))
477482
}
478483

484+
#[cfg(feature="async-ssl")]
485+
pub fn async_connect(
486+
self,
487+
ssl_config: Option<TlsConnector>,
488+
handle: &Handle,
489+
) -> async::ClientNew<Box<stream::async::Stream + Send>> {
490+
unimplemented!();
491+
}
492+
479493
#[cfg(feature="async-ssl")]
480494
pub fn async_connect_secure(
481495
self,
@@ -542,7 +556,7 @@ impl<'u> ClientBuilder<'u> {
542556

543557
#[cfg(feature="async")]
544558
pub fn async_connect_on<S>(self, stream: S) -> async::ClientNew<S>
545-
where S: stream::AsyncStream + Send + 'static
559+
where S: stream::async::Stream + Send + 'static
546560
{
547561
let mut builder = ClientBuilder {
548562
url: Cow::Owned(self.url.into_owned()),
@@ -695,7 +709,7 @@ impl<'u> ClientBuilder<'u> {
695709
Ok(TcpStream::connect(self.extract_host_port(secure)?)?)
696710
}
697711

698-
#[cfg(any(feature="ssl", feature="async-ssl"))]
712+
#[cfg(any(feature="sync-ssl", feature="async-ssl"))]
699713
fn extract_host_ssl_conn(
700714
&self,
701715
connector: Option<TlsConnector>,
@@ -711,7 +725,7 @@ impl<'u> ClientBuilder<'u> {
711725
Ok((host, connector))
712726
}
713727

714-
#[cfg(feature="ssl")]
728+
#[cfg(feature="sync-ssl")]
715729
fn wrap_ssl(
716730
&self,
717731
tcp_stream: TcpStream,

0 commit comments

Comments
 (0)