1
1
//! Postgres support for the `r2d2` connection pool.
2
- #![ doc( html_root_url="https://sfackler.github.io/r2d2-postgres/doc/v0.9.3 " ) ]
2
+ #![ doc( html_root_url="https://sfackler.github.io/r2d2-postgres/doc/v0.10.0 " ) ]
3
3
#![ warn( missing_docs) ]
4
4
extern crate r2d2;
5
5
extern crate postgres;
6
6
7
7
use std:: error;
8
8
use std:: error:: Error as _StdError;
9
9
use std:: fmt;
10
- use postgres:: { IntoConnectParams , SslMode } ;
10
+ use postgres:: { IntoConnectParams } ;
11
+ use postgres:: io:: NegotiateSsl ;
12
+
13
+ /// Like `postgres::SslMode` except that it owns its `NegotiateSsl` instance.
14
+ #[ derive( Debug ) ]
15
+ pub enum SslMode {
16
+ /// Like `postgres::SslMode::None`.
17
+ None ,
18
+ /// Like `postgres::SslMode::Prefer`.
19
+ Prefer ( Box < NegotiateSsl +Sync +Send > ) ,
20
+ /// Like `postgres::SslMode::Require`.
21
+ Require ( Box < NegotiateSsl +Sync +Send > ) ,
22
+ }
11
23
12
24
/// A unified enum of errors returned by postgres::Connection
13
25
#[ derive( Debug ) ]
@@ -45,22 +57,18 @@ impl error::Error for Error {
45
57
/// ## Example
46
58
///
47
59
/// ```rust,no_run
48
- /// #![allow(unstable)]
49
60
/// extern crate r2d2;
50
61
/// extern crate r2d2_postgres;
51
62
/// extern crate postgres;
52
63
///
53
- /// use std::sync::Arc;
54
- /// use std::default::Default;
55
64
/// use std::thread;
56
- /// use postgres::SslMode;
57
- /// use r2d2_postgres::PostgresConnectionManager;
65
+ /// use r2d2_postgres::{SslMode, PostgresConnectionManager};
58
66
///
59
67
/// fn main() {
60
68
/// let config = r2d2::Config::default();
61
69
/// let manager = PostgresConnectionManager::new("postgres://postgres@localhost",
62
70
/// SslMode::None).unwrap();
63
- /// let pool =Arc::new( r2d2::Pool::new(config, manager).unwrap() );
71
+ /// let pool = r2d2::Pool::new(config, manager).unwrap();
64
72
///
65
73
/// for i in 0..10i32 {
66
74
/// let pool = pool.clone();
@@ -86,7 +94,7 @@ impl PostgresConnectionManager {
86
94
->Result < PostgresConnectionManager , postgres:: error:: ConnectError > {
87
95
let params =match params. into_connect_params ( ) {
88
96
Ok ( params) => params,
89
- Err ( err) =>return Err ( postgres:: error:: ConnectError :: BadConnectParams ( err) ) ,
97
+ Err ( err) =>return Err ( postgres:: error:: ConnectError :: ConnectParams ( err) ) ,
90
98
} ;
91
99
92
100
Ok ( PostgresConnectionManager {
@@ -101,7 +109,12 @@ impl r2d2::ManageConnection for PostgresConnectionManager {
101
109
type Error =Error ;
102
110
103
111
fn connect ( & self ) ->Result < postgres:: Connection , Error > {
104
- postgres:: Connection :: connect ( self . params . clone ( ) , & self . ssl_mode ) . map_err ( Error :: Connect )
112
+ let mode =match self . ssl_mode {
113
+ SslMode :: None => postgres:: SslMode :: None ,
114
+ SslMode :: Prefer ( ref n) => postgres:: SslMode :: Prefer ( & * * n) ,
115
+ SslMode :: Require ( ref n) => postgres:: SslMode :: Require ( & * * n) ,
116
+ } ;
117
+ postgres:: Connection :: connect ( self . params . clone ( ) , mode) . map_err ( Error :: Connect )
105
118
}
106
119
107
120
fn is_valid ( & self , conn : & mut postgres:: Connection ) ->Result < ( ) , Error > {