Skip to content

Commit 32f67c5

Browse files
committed
Switch things to newtypes
The bug I thought was blocking this wasn't actually.
1 parent 83ad914 commit 32f67c5

2 files changed

Lines changed: 13 additions & 21 deletions

File tree

src/postgres/lib.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,7 @@ impl InnerPostgresConnection {
421421
}
422422

423423
/// A connection to a Postgres database.
424-
// FIXME should be a newtype
425-
pub struct PostgresConnection {
426-
priv conn: Cell<InnerPostgresConnection>
427-
}
424+
pub struct PostgresConnection(Cell<InnerPostgresConnection>);
428425

429426
impl PostgresConnection {
430427
/// Attempts to create a new connection to a Postgres database.
@@ -441,7 +438,7 @@ impl PostgresConnection {
441438
pub fn try_connect(url: &str) -> Result<PostgresConnection,
442439
PostgresConnectError> {
443440
do InnerPostgresConnection::try_connect(url).map_move |conn| {
444-
PostgresConnection { conn: Cell::new(conn) }
441+
PostgresConnection(Cell::new(conn))
445442
}
446443
}
447444

@@ -458,9 +455,9 @@ impl PostgresConnection {
458455
/// Sets the notice handler for the connection, returning the old handler.
459456
pub fn set_notice_handler(&self, handler: ~PostgresNoticeHandler)
460457
-> ~PostgresNoticeHandler {
461-
let mut conn = self.conn.take();
458+
let mut conn = self.take();
462459
let handler = conn.set_notice_handler(handler);
463-
self.conn.put_back(conn);
460+
self.put_back(conn);
464461
handler
465462
}
466463

@@ -474,7 +471,7 @@ impl PostgresConnection {
474471
/// not outlive that connection.
475472
pub fn try_prepare<'a>(&'a self, query: &str)
476473
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
477-
do self.conn.with_mut_ref |conn| {
474+
do self.with_mut_ref |conn| {
478475
conn.try_prepare(query, self)
479476
}
480477
}
@@ -530,7 +527,7 @@ impl PostgresConnection {
530527
}
531528

532529
fn quick_query(&self, query: &str) {
533-
do self.conn.with_mut_ref |conn| {
530+
do self.with_mut_ref |conn| {
534531
conn.write_messages([&Query { query: query }]);
535532

536533
loop {
@@ -546,19 +543,19 @@ impl PostgresConnection {
546543
}
547544

548545
fn wait_for_ready(&self) {
549-
do self.conn.with_mut_ref |conn| {
546+
do self.with_mut_ref |conn| {
550547
conn.wait_for_ready()
551548
}
552549
}
553550

554551
fn read_message(&self) -> BackendMessage {
555-
do self.conn.with_mut_ref |conn| {
552+
do self.with_mut_ref |conn| {
556553
conn.read_message()
557554
}
558555
}
559556

560557
fn write_messages(&self, messages: &[&FrontendMessage]) {
561-
do self.conn.with_mut_ref |conn| {
558+
do self.with_mut_ref |conn| {
562559
conn.write_messages(messages)
563560
}
564561
}

src/postgres/pool/mod.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ impl InnerConnectionPool {
2929
/// A simple fixed-size Postgres connection pool.
3030
///
3131
/// It can be shared across tasks.
32-
// Should be a newtype, but blocked by mozilla/rust#9155
3332
#[deriving(Clone)]
34-
pub struct PostgresConnectionPool {
35-
priv pool: MutexArc<InnerConnectionPool>
36-
}
33+
pub struct PostgresConnectionPool(MutexArc<InnerConnectionPool>);
3734

3835
impl PostgresConnectionPool {
3936
/// Attempts to create a new pool with the specified number of connections.
@@ -54,9 +51,7 @@ impl PostgresConnectionPool {
5451
}
5552
}
5653

57-
Ok(PostgresConnectionPool {
58-
pool: MutexArc::new(pool)
59-
})
54+
Ok(PostgresConnectionPool(MutexArc::new(pool)))
6055
}
6156

6257
/// A convenience function wrapping `try_new`.
@@ -74,7 +69,7 @@ impl PostgresConnectionPool {
7469
/// If all connections are in use, blocks until one becomes available.
7570
pub fn get_connection(&self) -> PooledPostgresConnection {
7671
let conn = unsafe {
77-
do self.pool.unsafe_access_cond |pool, cvar| {
72+
do self.unsafe_access_cond |pool, cvar| {
7873
while pool.pool.is_empty() {
7974
cvar.wait();
8075
}
@@ -103,7 +98,7 @@ pub struct PooledPostgresConnection {
10398
impl Drop for PooledPostgresConnection {
10499
fn drop(&mut self) {
105100
unsafe {
106-
do self.pool.pool.unsafe_access |pool| {
101+
do self.pool.unsafe_access |pool| {
107102
pool.pool.push(self.conn.take_unwrap());
108103
}
109104
}

0 commit comments

Comments
 (0)