Skip to content

Commit f957f10

Browse files
committed
Use a Box<NegotiateSsl> instead of a default param
Default parameter's aren't used in type inference yet, so the old setup would result in overly verbose things like &mut SslMode::None::<NoSsl>. In the future we can add the parameter back with a default of Box<NegotiateSsl> to avoid forcing people to box stuff.
1 parent 2d8f688 commit f957f10

File tree

5 files changed

+28
-45
lines changed

5 files changed

+28
-45
lines changed

src/io/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,3 @@ pub trait NegotiateSsl {
2626
fn negotiate_ssl(&mut self, host: &str, stream: Stream)
2727
-> Result<Box<StreamWrapper>, Box<Error>>;
2828
}
29-
30-
/// An uninhabited type implementing `NegotiateSsl`.
31-
///
32-
/// `NoSsl` cannot be instantiated, so the only `SslMode<NoSslMode>` value that
33-
/// can exist is `SslMode::None`. `NoSsl` is the default value of `SslMode`'s
34-
/// parameter so `&mut SslMode::None` can always be passed into
35-
/// `Connection::connect` even if no SSL implementation is available.
36-
pub enum NoSsl {}
37-
38-
impl NegotiateSsl for NoSsl {
39-
fn negotiate_ssl(&mut self, _: &str, _: Stream) -> Result<Box<StreamWrapper>, Box<Error>> {
40-
match *self {}
41-
}
42-
}

src/lib.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! }
1616
//!
1717
//! fn main() {
18-
//! let conn = Connection::connect("postgresql://postgres@localhost", &SslMode::None)
18+
//! let conn = Connection::connect("postgresql://postgres@localhost", &mut SslMode::None)
1919
//! .unwrap();
2020
//!
2121
//! conn.execute("CREATE TABLE person (
@@ -79,7 +79,7 @@ use std::path::PathBuf;
7979
pub use error::{Error, ConnectError, SqlState, DbError, ErrorPosition};
8080
#[doc(inline)]
8181
pub use types::{Oid, Type, Kind, ToSql, FromSql};
82-
use io::{NoSsl, StreamWrapper, NegotiateSsl};
82+
use io::{StreamWrapper, NegotiateSsl};
8383
use types::IsNull;
8484
#[doc(inline)]
8585
pub use types::Slice;
@@ -379,17 +379,17 @@ pub struct CancelData {
379379
/// # use postgres::{Connection, SslMode};
380380
/// # use std::thread;
381381
/// # let url = "";
382-
/// let conn = Connection::connect(url, &SslMode::None).unwrap();
382+
/// let conn = Connection::connect(url, &mut SslMode::None).unwrap();
383383
/// let cancel_data = conn.cancel_data();
384384
/// thread::spawn(move || {
385385
/// conn.execute("SOME EXPENSIVE QUERY", &[]).unwrap();
386386
/// });
387387
/// # let _ =
388-
/// postgres::cancel_query(url, &SslMode::None, cancel_data);
388+
/// postgres::cancel_query(url, &mut SslMode::None, cancel_data);
389389
/// ```
390-
pub fn cancel_query<T, N>(params: T, ssl: &mut SslMode<N>, data: CancelData)
390+
pub fn cancel_query<T>(params: T, ssl: &mut SslMode, data: CancelData)
391391
-> result::Result<(), ConnectError>
392-
where T: IntoConnectParams, N: NegotiateSsl {
392+
where T: IntoConnectParams {
393393
let params = try!(params.into_connect_params());
394394
let mut socket = try!(priv_io::initialize_stream(&params, ssl));
395395

@@ -458,13 +458,13 @@ impl IsolationLevel {
458458
}
459459

460460
/// Specifies the SSL support requested for a new connection.
461-
pub enum SslMode<N: NegotiateSsl = NoSsl> {
461+
pub enum SslMode {
462462
/// The connection will not use SSL.
463463
None,
464464
/// The connection will use SSL if the backend supports it.
465-
Prefer(N),
465+
Prefer(Box<NegotiateSsl>),
466466
/// The connection must use SSL.
467-
Require(N),
467+
Require(Box<NegotiateSsl>),
468468
}
469469

470470
#[derive(Clone)]
@@ -497,9 +497,8 @@ impl Drop for InnerConnection {
497497
}
498498

499499
impl InnerConnection {
500-
fn connect<T, N>(params: T, ssl: &mut SslMode<N>)
501-
-> result::Result<InnerConnection, ConnectError>
502-
where T: IntoConnectParams, N: NegotiateSsl {
500+
fn connect<T>(params: T, ssl: &mut SslMode) -> result::Result<InnerConnection, ConnectError>
501+
where T: IntoConnectParams {
503502
let params = try!(params.into_connect_params());
504503
let stream = try!(priv_io::initialize_stream(&params, ssl));
505504

@@ -987,15 +986,15 @@ impl Connection {
987986
/// # use postgres::{Connection, SslMode, ConnectError};
988987
/// # fn f() -> Result<(), ConnectError> {
989988
/// let url = "postgresql://postgres:hunter2@localhost:2994/foodb";
990-
/// let conn = try!(Connection::connect(url, &SslMode::None));
989+
/// let conn = try!(Connection::connect(url, &mut SslMode::None));
991990
/// # Ok(()) };
992991
/// ```
993992
///
994993
/// ```rust,no_run
995994
/// # use postgres::{Connection, SslMode, ConnectError};
996995
/// # fn f() -> Result<(), ConnectError> {
997996
/// let url = "postgresql://postgres@%2Frun%2Fpostgres";
998-
/// let conn = try!(Connection::connect(url, &SslMode::None));
997+
/// let conn = try!(Connection::connect(url, &mut SslMode::None));
999998
/// # Ok(()) };
1000999
/// ```
10011000
///
@@ -1015,12 +1014,11 @@ impl Connection {
10151014
/// database: None,
10161015
/// options: vec![],
10171016
/// };
1018-
/// let conn = try!(Connection::connect(params, &SslMode::None));
1017+
/// let conn = try!(Connection::connect(params, &mut SslMode::None));
10191018
/// # Ok(()) };
10201019
/// ```
1021-
pub fn connect<T, N>(params: T, ssl: &mut SslMode<N>)
1022-
-> result::Result<Connection, ConnectError>
1023-
where T: IntoConnectParams, N: NegotiateSsl {
1020+
pub fn connect<T>(params: T, ssl: &mut SslMode) -> result::Result<Connection, ConnectError>
1021+
where T: IntoConnectParams {
10241022
InnerConnection::connect(params, ssl).map(|conn| {
10251023
Connection { conn: RefCell::new(conn) }
10261024
})
@@ -1051,7 +1049,7 @@ impl Connection {
10511049
///
10521050
/// ```rust,no_run
10531051
/// # use postgres::{Connection, SslMode};
1054-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1052+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
10551053
/// let maybe_stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1");
10561054
/// let stmt = match maybe_stmt {
10571055
/// Ok(stmt) => stmt,
@@ -1074,7 +1072,7 @@ impl Connection {
10741072
/// # use postgres::{Connection, SslMode};
10751073
/// # fn f() -> postgres::Result<()> {
10761074
/// # let x = 10i32;
1077-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1075+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
10781076
/// let stmt = try!(conn.prepare_cached("SELECT foo FROM bar WHERE baz = $1"));
10791077
/// for row in try!(stmt.query(&[&x])) {
10801078
/// println!("foo: {}", row.get::<_, String>(0));
@@ -1113,7 +1111,7 @@ impl Connection {
11131111
/// ```rust,no_run
11141112
/// # use postgres::{Connection, SslMode};
11151113
/// # fn foo() -> Result<(), postgres::Error> {
1116-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1114+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
11171115
/// let trans = try!(conn.transaction());
11181116
/// try!(trans.execute("UPDATE foo SET bar = 10", &[]));
11191117
/// // ...
@@ -1501,7 +1499,7 @@ impl<'conn> Statement<'conn> {
15011499
///
15021500
/// ```rust,no_run
15031501
/// # use postgres::{Connection, SslMode};
1504-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1502+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
15051503
/// # let bar = 1i32;
15061504
/// # let baz = true;
15071505
/// let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2").unwrap();
@@ -1560,7 +1558,7 @@ impl<'conn> Statement<'conn> {
15601558
///
15611559
/// ```rust,no_run
15621560
/// # use postgres::{Connection, SslMode};
1563-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1561+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
15641562
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap();
15651563
/// # let baz = true;
15661564
/// let rows = match stmt.query(&[&baz]) {
@@ -1874,7 +1872,7 @@ impl<'a> Row<'a> {
18741872
///
18751873
/// ```rust,no_run
18761874
/// # use postgres::{Connection, SslMode};
1877-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1875+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
18781876
/// # let stmt = conn.prepare("").unwrap();
18791877
/// # let mut result = stmt.query(&[]).unwrap();
18801878
/// # let row = result.iter().next().unwrap();

src/priv_io.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ fn open_socket(params: &ConnectParams) -> Result<InternalStream, ConnectError> {
118118
}
119119
}
120120

121-
pub fn initialize_stream<N>(params: &ConnectParams, ssl: &mut SslMode<N>)
122-
-> Result<Box<StreamWrapper>, ConnectError>
123-
where N: NegotiateSsl {
121+
pub fn initialize_stream(params: &ConnectParams, ssl: &mut SslMode)
122+
-> Result<Box<StreamWrapper>, ConnectError> {
124123
let mut socket = Stream(try!(open_socket(params)));
125124

126125
let (ssl_required, negotiator) = match *ssl {

src/types/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use types::IsNull;
1616
/// ```rust,no_run
1717
/// # fn foo() -> postgres::Result<()> {
1818
/// # use postgres::{Connection, SslMode, Slice};
19-
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
19+
/// # let conn = Connection::connect("", &mut SslMode::None).unwrap();
2020
/// let values = &[1i32, 2, 3, 4, 5, 6];
2121
/// let stmt = try!(conn.prepare("SELECT * FROM foo WHERE id = ANY($1)"));
2222
/// for row in &try!(stmt.query(&[&Slice(values)])) {

tests/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ fn test_cancel_query() {
675675
fn test_require_ssl_conn() {
676676
let ctx = SslContext::new(SslMethod::Sslv23).unwrap();
677677
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
678-
&mut SslMode::Require(ctx)));
678+
&mut SslMode::Require(Box::new(ctx))));
679679
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
680680
}
681681

@@ -684,7 +684,7 @@ fn test_require_ssl_conn() {
684684
fn test_prefer_ssl_conn() {
685685
let ctx = SslContext::new(SslMethod::Sslv23).unwrap();
686686
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
687-
&mut SslMode::Prefer(ctx)));
687+
&mut SslMode::Prefer(Box::new(ctx))));
688688
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
689689
}
690690

@@ -837,7 +837,7 @@ fn test_copy_in_bad_type() {
837837

838838
#[test]
839839
fn test_copy_in_weird_names() {
840-
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
840+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &mut SslMode::None));
841841
or_panic!(conn.execute(r#"CREATE TEMPORARY TABLE "na""me" (U&" \\\+01F4A9" VARCHAR)"#, &[]));
842842
let stmt = or_panic!(conn.prepare_copy_in("na\"me", &[" \\💩"]));
843843
assert_eq!(&Type::Varchar, &stmt.column_types()[0]);

0 commit comments

Comments
 (0)