Skip to content

Commit

Permalink
Use mainline rust-postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 committed Mar 13, 2018
1 parent 8528968 commit 609cb7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 38 deletions.
4 changes: 2 additions & 2 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ authors = ["Kyle Huey <khuey@kylehuey.com>"]
[dependencies]
bb8 = { path = ".." }
futures = "0.1"
postgres-shared = { git = "https://github.com/khuey/rust-postgres", branch = "bb8-fixes" }
postgres-shared = { git = "https://github.com/sfackler/rust-postgres" }
tokio-core = "0.1"
tokio-postgres = { git = "https://github.com/khuey/rust-postgres", branch = "bb8-fixes" }
tokio-postgres = { git = "https://github.com/sfackler/rust-postgres" }
49 changes: 13 additions & 36 deletions postgres/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Postgres support for the `bb8` connection pool.
#![deny(missing_docs,missing_debug_implementations)]
#![deny(missing_docs)]

pub extern crate bb8;
pub extern crate tokio_postgres;
Expand All @@ -10,7 +10,7 @@ extern crate tokio_core;

use futures::Future;
use tokio_core::reactor::Handle;
use tokio_postgres::{Connection, Error};
use tokio_postgres::{Connection, Error, TlsMode};
use tokio_postgres::params::{ConnectParams, IntoConnectParams};
use tokio_postgres::tls::Handshake;

Expand All @@ -19,42 +19,23 @@ use std::io;

type Result<T> = std::result::Result<T, Error>;

/// Like `tokio_postgres::TlsMode` except that it owns its `Handshake` instance.
pub enum TlsMode {
/// Like `postgres::TlsMode::None`.
None,
/// Like `postgres::TlsMode::Prefer`.
Prefer(Box<Handshake + Sync + Send>),
/// Like `postgres::TlsMode::Require`.
Require(Box<Handshake + Sync + Send>),
}

impl fmt::Debug for TlsMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
TlsMode::None => &"TlsMode::None",
TlsMode::Prefer(_) => &"TlsMode::Prefer",
TlsMode::Require(_) => &"TlsMode::Require",
})
}
}

/// A `bb8::ManageConnection` for `tokio_postgres::Connection`s.
#[derive(Debug)]
pub struct PostgresConnectionManager {
params: ConnectParams,
tls_mode: TlsMode,
tls_mode: Box<Fn() -> TlsMode + Send + Sync>,
}

impl PostgresConnectionManager {
/// Create a new `PostgresConnectionManager`.
pub fn new<T>(params: T, tls_mode: TlsMode) -> Result<PostgresConnectionManager>
where T: IntoConnectParams
pub fn new<F, T>(params: T, tls_mode: F) -> Result<PostgresConnectionManager>
where
T: IntoConnectParams,
F: Fn() -> TlsMode + Send + Sync + 'static,
{
Ok(PostgresConnectionManager {
params: params.into_connect_params()
.map_err(postgres_shared::error::connect)?,
tls_mode: tls_mode,
tls_mode: Box::new(tls_mode),
})
}
}
Expand All @@ -63,15 +44,11 @@ impl bb8::ManageConnection for PostgresConnectionManager {
type Connection = Connection;
type Error = Error;

fn connect<'a>(&'a self,
handle: Handle)
-> Box<Future<Item = Self::Connection, Error = Self::Error> + 'a> {
let tls_mode = match self.tls_mode {
TlsMode::None => tokio_postgres::TlsMode::None,
TlsMode::Prefer(ref n) => tokio_postgres::TlsMode::Prefer(&**n),
TlsMode::Require(ref n) => tokio_postgres::TlsMode::Require(&**n),
};
Connection::connect(self.params.clone(), tls_mode, &handle)
fn connect<'a>(
&'a self,
handle: Handle,
) -> Box<Future<Item = Self::Connection, Error = Self::Error> + 'a> {
Connection::connect(self.params.clone(), (self.tls_mode)(), &handle)
}

fn is_valid
Expand Down

0 comments on commit 609cb7e

Please sign in to comment.