Add a nightly feature

Currently uses the UnixStream from the standard library
This commit is contained in:
Steven Fackler 2016-03-27 13:02:04 -07:00
parent bd60ecfc1f
commit b0bed6ab4c
5 changed files with 23 additions and 16 deletions

View File

@ -11,3 +11,4 @@ before_script:
script:
- cargo test
- cargo test --features "uuid rustc-serialize time unix_socket serde_json chrono openssl bit-vec eui48"
- (test $TRAVIS_RUST_VERSION != "nightly" || cargo test --features nightly)

View File

@ -20,6 +20,9 @@ bench = false
name = "test"
path = "tests/test.rs"
[features]
nightly = []
[dependencies]
bufstream = "0.1"
byteorder = "0.5"

View File

@ -41,6 +41,7 @@
#![doc(html_root_url="https://sfackler.github.io/rust-postgres/doc/v0.11.4")]
#![warn(missing_docs)]
#![allow(unknown_lints, needless_lifetimes)] // for clippy
#![cfg_attr(feature = "nightly", feature(unix_socket))]
extern crate bufstream;
extern crate byteorder;
@ -68,7 +69,7 @@ use std::mem;
use std::result;
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
use std::path::PathBuf;
// FIXME remove in 0.12
@ -115,8 +116,8 @@ pub enum ConnectTarget {
Tcp(String),
/// Connect via a Unix domain socket in the specified directory.
///
/// Requires the `unix_socket` feature.
#[cfg(feature = "unix_socket")]
/// Requires the `unix_socket` or `nightly` feature.
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
Unix(PathBuf),
}
@ -173,12 +174,12 @@ impl<'a> IntoConnectParams for &'a str {
impl IntoConnectParams for Url {
fn into_connect_params(self) -> result::Result<ConnectParams, Box<StdError + StdSync + Send>> {
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
fn make_unix(maybe_path: String)
-> result::Result<ConnectTarget, Box<StdError + StdSync + Send>> {
Ok(ConnectTarget::Unix(PathBuf::from(maybe_path)))
}
#[cfg(not(feature = "unix_socket"))]
#[cfg(not(any(feature = "unix_socket", feature = "nightly")))]
fn make_unix(_: String) -> result::Result<ConnectTarget, Box<StdError + StdSync + Send>> {
Err("unix socket support requires the `unix_socket` feature".into())
}

View File

@ -9,6 +9,8 @@ use std::time::Duration;
use bufstream::BufStream;
#[cfg(feature = "unix_socket")]
use unix_socket::UnixStream;
#[cfg(all(not(feature = "unix_socket"), feature = "nightly"))]
use std::os::unix::net::UnixStream;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(windows)]
@ -32,7 +34,7 @@ impl StreamOptions for BufStream<Box<StreamWrapper>> {
fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
match self.get_ref().get_ref().0 {
InternalStream::Tcp(ref s) => s.set_read_timeout(timeout),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref s) => s.set_read_timeout(timeout),
}
}
@ -40,7 +42,7 @@ impl StreamOptions for BufStream<Box<StreamWrapper>> {
fn set_nonblocking(&self, nonblock: bool) -> io::Result<()> {
match self.get_ref().get_ref().0 {
InternalStream::Tcp(ref s) => s.set_nonblocking(nonblock),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref s) => s.set_nonblocking(nonblock),
}
}
@ -56,7 +58,7 @@ impl fmt::Debug for Stream {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
InternalStream::Tcp(ref s) => fmt::Debug::fmt(s, fmt),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref s) => fmt::Debug::fmt(s, fmt),
}
}
@ -93,7 +95,7 @@ impl AsRawFd for Stream {
fn as_raw_fd(&self) -> RawFd {
match self.0 {
InternalStream::Tcp(ref s) => s.as_raw_fd(),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref s) => s.as_raw_fd(),
}
}
@ -111,7 +113,7 @@ impl AsRawSocket for Stream {
enum InternalStream {
Tcp(TcpStream),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
Unix(UnixStream),
}
@ -119,7 +121,7 @@ impl Read for InternalStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match *self {
InternalStream::Tcp(ref mut s) => s.read(buf),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref mut s) => s.read(buf),
}
}
@ -129,7 +131,7 @@ impl Write for InternalStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match *self {
InternalStream::Tcp(ref mut s) => s.write(buf),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref mut s) => s.write(buf),
}
}
@ -137,7 +139,7 @@ impl Write for InternalStream {
fn flush(&mut self) -> io::Result<()> {
match *self {
InternalStream::Tcp(ref mut s) => s.flush(),
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
InternalStream::Unix(ref mut s) => s.flush(),
}
}
@ -149,7 +151,7 @@ fn open_socket(params: &ConnectParams) -> Result<InternalStream, ConnectError> {
ConnectTarget::Tcp(ref host) => {
Ok(try!(TcpStream::connect(&(&**host, port)).map(InternalStream::Tcp)))
}
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
ConnectTarget::Unix(ref path) => {
let path = path.join(&format!(".s.PGSQL.{}", port));
Ok(try!(UnixStream::connect(&path).map(InternalStream::Unix)))
@ -183,7 +185,7 @@ pub fn initialize_stream(params: &ConnectParams,
// Postgres doesn't support SSL over unix sockets
let host = match params.target {
ConnectTarget::Tcp(ref host) => host,
#[cfg(feature = "unix_socket")]
#[cfg(any(feature = "unix_socket", feature = "nightly"))]
ConnectTarget::Unix(_) => return Err(ConnectError::Io(::bad_response())),
};

View File

@ -75,7 +75,7 @@ fn test_connection_finish() {
}
#[test]
#[cfg_attr(not(feature = "unix_socket"), ignore)]
#[cfg_attr(not(any(feature = "unix_socket", feature = "nightly")), ignore)]
fn test_unix_connection() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
let stmt = or_panic!(conn.prepare("SHOW unix_socket_directories"));