Switch things to newtypes
The bug I thought was blocking this wasn't actually.
This commit is contained in:
@@ -421,10 +421,7 @@ impl InnerPostgresConnection {
|
||||
}
|
||||
|
||||
/// A connection to a Postgres database.
|
||||
// FIXME should be a newtype
|
||||
pub struct PostgresConnection {
|
||||
priv conn: Cell<InnerPostgresConnection>
|
||||
}
|
||||
pub struct PostgresConnection(Cell<InnerPostgresConnection>);
|
||||
|
||||
impl PostgresConnection {
|
||||
/// Attempts to create a new connection to a Postgres database.
|
||||
@@ -441,7 +438,7 @@ impl PostgresConnection {
|
||||
pub fn try_connect(url: &str) -> Result<PostgresConnection,
|
||||
PostgresConnectError> {
|
||||
do InnerPostgresConnection::try_connect(url).map_move |conn| {
|
||||
PostgresConnection { conn: Cell::new(conn) }
|
||||
PostgresConnection(Cell::new(conn))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,9 +455,9 @@ impl PostgresConnection {
|
||||
/// Sets the notice handler for the connection, returning the old handler.
|
||||
pub fn set_notice_handler(&self, handler: ~PostgresNoticeHandler)
|
||||
-> ~PostgresNoticeHandler {
|
||||
let mut conn = self.conn.take();
|
||||
let mut conn = self.take();
|
||||
let handler = conn.set_notice_handler(handler);
|
||||
self.conn.put_back(conn);
|
||||
self.put_back(conn);
|
||||
handler
|
||||
}
|
||||
|
||||
@@ -474,7 +471,7 @@ impl PostgresConnection {
|
||||
/// not outlive that connection.
|
||||
pub fn try_prepare<'a>(&'a self, query: &str)
|
||||
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
|
||||
do self.conn.with_mut_ref |conn| {
|
||||
do self.with_mut_ref |conn| {
|
||||
conn.try_prepare(query, self)
|
||||
}
|
||||
}
|
||||
@@ -530,7 +527,7 @@ impl PostgresConnection {
|
||||
}
|
||||
|
||||
fn quick_query(&self, query: &str) {
|
||||
do self.conn.with_mut_ref |conn| {
|
||||
do self.with_mut_ref |conn| {
|
||||
conn.write_messages([&Query { query: query }]);
|
||||
|
||||
loop {
|
||||
@@ -546,19 +543,19 @@ impl PostgresConnection {
|
||||
}
|
||||
|
||||
fn wait_for_ready(&self) {
|
||||
do self.conn.with_mut_ref |conn| {
|
||||
do self.with_mut_ref |conn| {
|
||||
conn.wait_for_ready()
|
||||
}
|
||||
}
|
||||
|
||||
fn read_message(&self) -> BackendMessage {
|
||||
do self.conn.with_mut_ref |conn| {
|
||||
do self.with_mut_ref |conn| {
|
||||
conn.read_message()
|
||||
}
|
||||
}
|
||||
|
||||
fn write_messages(&self, messages: &[&FrontendMessage]) {
|
||||
do self.conn.with_mut_ref |conn| {
|
||||
do self.with_mut_ref |conn| {
|
||||
conn.write_messages(messages)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,8 @@ impl InnerConnectionPool {
|
||||
/// A simple fixed-size Postgres connection pool.
|
||||
///
|
||||
/// It can be shared across tasks.
|
||||
// Should be a newtype, but blocked by mozilla/rust#9155
|
||||
#[deriving(Clone)]
|
||||
pub struct PostgresConnectionPool {
|
||||
priv pool: MutexArc<InnerConnectionPool>
|
||||
}
|
||||
pub struct PostgresConnectionPool(MutexArc<InnerConnectionPool>);
|
||||
|
||||
impl PostgresConnectionPool {
|
||||
/// Attempts to create a new pool with the specified number of connections.
|
||||
@@ -54,9 +51,7 @@ impl PostgresConnectionPool {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PostgresConnectionPool {
|
||||
pool: MutexArc::new(pool)
|
||||
})
|
||||
Ok(PostgresConnectionPool(MutexArc::new(pool)))
|
||||
}
|
||||
|
||||
/// A convenience function wrapping `try_new`.
|
||||
@@ -74,7 +69,7 @@ impl PostgresConnectionPool {
|
||||
/// If all connections are in use, blocks until one becomes available.
|
||||
pub fn get_connection(&self) -> PooledPostgresConnection {
|
||||
let conn = unsafe {
|
||||
do self.pool.unsafe_access_cond |pool, cvar| {
|
||||
do self.unsafe_access_cond |pool, cvar| {
|
||||
while pool.pool.is_empty() {
|
||||
cvar.wait();
|
||||
}
|
||||
@@ -103,7 +98,7 @@ pub struct PooledPostgresConnection {
|
||||
impl Drop for PooledPostgresConnection {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
do self.pool.pool.unsafe_access |pool| {
|
||||
do self.pool.unsafe_access |pool| {
|
||||
pool.pool.push(self.conn.take_unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user