diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index acb8f596..46423cbd 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -28,7 +28,9 @@ postgres-shared = { version = "0.2", path = "../postgres-shared" } postgres-protocol = "0.2" tokio-core = "0.1" tokio-dns-unofficial = "0.1" -tokio-uds = "0.1" tokio-openssl = { version = "0.1", optional = true } openssl = { version = "0.9", optional = true } + +[target.'cfg(unix)'.dependencies] +tokio-uds = "0.1" diff --git a/tokio-postgres/src/lib.rs b/tokio-postgres/src/lib.rs index 46988058..a8396a19 100644 --- a/tokio-postgres/src/lib.rs +++ b/tokio-postgres/src/lib.rs @@ -59,6 +59,8 @@ extern crate postgres_shared; extern crate postgres_protocol; extern crate tokio_core; extern crate tokio_dns; + +#[cfg(unix)] extern crate tokio_uds; #[cfg(feature = "tokio-openssl")] diff --git a/tokio-postgres/src/stream.rs b/tokio-postgres/src/stream.rs index 59965914..3833be1b 100644 --- a/tokio-postgres/src/stream.rs +++ b/tokio-postgres/src/stream.rs @@ -8,6 +8,8 @@ use tokio_core::io::{Io, Codec, EasyBuf, Framed}; use tokio_core::net::TcpStream; use tokio_core::reactor::Handle; use tokio_dns; + +#[cfg(unix)] use tokio_uds::UnixStream; use TlsMode; @@ -26,12 +28,17 @@ pub fn connect(host: Host, Either::A(tokio_dns::tcp_connect((&**host, port), handle.remote().clone()) .map(|s| Stream(InnerStream::Tcp(s)))) } + #[cfg(unix)] Host::Unix(ref host) => { let addr = host.join(format!(".s.PGSQL.{}", port)); Either::B(UnixStream::connect(addr, handle) .map(|s| Stream(InnerStream::Unix(s))) .into_future()) } + #[cfg(not(unix))] + Host::Unix(_) => { + Either::B(Err(ConnectError::ConnectParams("unix sockets are not supported on this platform")).into_future()) + } }; let (required, handshaker) = match tls_mode { @@ -84,6 +91,7 @@ pub struct Stream(InnerStream); enum InnerStream { Tcp(TcpStream), + #[cfg(unix)] Unix(UnixStream), } @@ -91,6 +99,7 @@ impl Read for Stream { fn read(&mut self, buf: &mut [u8]) -> io::Result { match self.0 { InnerStream::Tcp(ref mut s) => s.read(buf), + #[cfg(unix)] InnerStream::Unix(ref mut s) => s.read(buf), } } @@ -100,6 +109,7 @@ impl Write for Stream { fn write(&mut self, buf: &[u8]) -> io::Result { match self.0 { InnerStream::Tcp(ref mut s) => s.write(buf), + #[cfg(unix)] InnerStream::Unix(ref mut s) => s.write(buf), } } @@ -107,6 +117,7 @@ impl Write for Stream { fn flush(&mut self) -> io::Result<()> { match self.0 { InnerStream::Tcp(ref mut s) => s.flush(), + #[cfg(unix)] InnerStream::Unix(ref mut s) => s.flush(), } } @@ -116,6 +127,7 @@ impl Io for Stream { fn poll_read(&mut self) -> Async<()> { match self.0 { InnerStream::Tcp(ref mut s) => s.poll_read(), + #[cfg(unix)] InnerStream::Unix(ref mut s) => s.poll_read(), } } @@ -123,6 +135,7 @@ impl Io for Stream { fn poll_write(&mut self) -> Async<()> { match self.0 { InnerStream::Tcp(ref mut s) => s.poll_write(), + #[cfg(unix)] InnerStream::Unix(ref mut s) => s.poll_write(), } }