Skip to content

Commit b06769b

Browse files
committed
Remove newtype usage
Newtype structs can't hide their internals, so it seems best to not use them.
1 parent df9a348 commit b06769b

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

src/postgres/lib.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ impl InnerPostgresConnection {
477477
}
478478

479479
/// A connection to a Postgres database.
480-
pub struct PostgresConnection(Cell<InnerPostgresConnection>);
480+
pub struct PostgresConnection {
481+
priv conn: Cell<InnerPostgresConnection>
482+
}
481483

482484
impl PostgresConnection {
483485
/// Attempts to create a new connection to a Postgres database.
@@ -494,7 +496,9 @@ impl PostgresConnection {
494496
pub fn try_connect(url: &str) -> Result<PostgresConnection,
495497
PostgresConnectError> {
496498
do InnerPostgresConnection::try_connect(url).map_move |conn| {
497-
PostgresConnection(Cell::new(conn))
499+
PostgresConnection {
500+
conn: Cell::new(conn)
501+
}
498502
}
499503
}
500504

@@ -511,9 +515,9 @@ impl PostgresConnection {
511515
/// Sets the notice handler for the connection, returning the old handler.
512516
pub fn set_notice_handler(&self, handler: ~PostgresNoticeHandler)
513517
-> ~PostgresNoticeHandler {
514-
let mut conn = self.take();
518+
let mut conn = self.conn.take();
515519
let handler = conn.set_notice_handler(handler);
516-
self.put_back(conn);
520+
self.conn.put_back(conn);
517521
handler
518522
}
519523

@@ -527,7 +531,7 @@ impl PostgresConnection {
527531
/// not outlive that connection.
528532
pub fn try_prepare<'a>(&'a self, query: &str)
529533
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
530-
do self.with_mut_ref |conn| {
534+
do self.conn.with_mut_ref |conn| {
531535
conn.try_prepare(query, self)
532536
}
533537
}
@@ -583,7 +587,7 @@ impl PostgresConnection {
583587
}
584588

585589
fn quick_query(&self, query: &str) {
586-
do self.with_mut_ref |conn| {
590+
do self.conn.with_mut_ref |conn| {
587591
conn.write_messages([&Query { query: query }]);
588592

589593
loop {
@@ -599,19 +603,19 @@ impl PostgresConnection {
599603
}
600604

601605
fn wait_for_ready(&self) {
602-
do self.with_mut_ref |conn| {
606+
do self.conn.with_mut_ref |conn| {
603607
conn.wait_for_ready()
604608
}
605609
}
606610

607611
fn read_message(&self) -> BackendMessage {
608-
do self.with_mut_ref |conn| {
612+
do self.conn.with_mut_ref |conn| {
609613
conn.read_message()
610614
}
611615
}
612616

613617
fn write_messages(&self, messages: &[&FrontendMessage]) {
614-
do self.with_mut_ref |conn| {
618+
do self.conn.with_mut_ref |conn| {
615619
conn.write_messages(messages)
616620
}
617621
}
@@ -649,13 +653,19 @@ impl<'self> PostgresTransaction<'self> {
649653
/// Like `PostgresConnection::try_prepare`.
650654
pub fn try_prepare<'a>(&'a self, query: &str)
651655
-> Result<TransactionalPostgresStatement<'a>, PostgresDbError> {
652-
self.conn.try_prepare(query).map_move(TransactionalPostgresStatement)
656+
do self.conn.try_prepare(query).map_move |stmt| {
657+
TransactionalPostgresStatement {
658+
stmt: stmt
659+
}
660+
}
653661
}
654662

655663
/// Like `PostgresConnection::prepare`.
656664
pub fn prepare<'a>(&'a self, query: &str)
657665
-> TransactionalPostgresStatement<'a> {
658-
TransactionalPostgresStatement(self.conn.prepare(query))
666+
TransactionalPostgresStatement {
667+
stmt: self.conn.prepare(query)
668+
}
659669
}
660670

661671
/// Like `PostgresConnection::try_update`.
@@ -929,28 +939,30 @@ impl ResultDescription {
929939
/// A statement prepared inside of a transaction.
930940
///
931941
/// Provides additional functionality over a `NormalPostgresStatement`.
932-
pub struct TransactionalPostgresStatement<'self>(NormalPostgresStatement<'self>);
942+
pub struct TransactionalPostgresStatement<'self> {
943+
priv stmt: NormalPostgresStatement<'self>
944+
}
933945

934946
impl<'self> PostgresStatement for TransactionalPostgresStatement<'self> {
935947
fn param_types<'a>(&'a self) -> &'a [PostgresType] {
936-
(**self).param_types()
948+
self.stmt.param_types()
937949
}
938950

939951
fn result_descriptions<'a>(&'a self) -> &'a [ResultDescription] {
940-
(**self).result_descriptions()
952+
self.stmt.result_descriptions()
941953
}
942954

943955
fn try_update(&self, params: &[&ToSql]) -> Result<uint, PostgresDbError> {
944-
(**self).try_update(params)
956+
self.stmt.try_update(params)
945957
}
946958

947959
fn try_query<'a>(&'a self, params: &[&ToSql])
948960
-> Result<PostgresResult<'a>, PostgresDbError> {
949-
(**self).try_query(params)
961+
self.stmt.try_query(params)
950962
}
951963

952964
fn find_col_named(&self, col: &str) -> Option<uint> {
953-
(**self).find_col_named(col)
965+
self.stmt.find_col_named(col)
954966
}
955967
}
956968

@@ -966,7 +978,7 @@ impl<'self> TransactionalPostgresStatement<'self> {
966978
/// the parameters of the statement.
967979
pub fn try_lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql])
968980
-> Result<PostgresResult<'a>, PostgresDbError> {
969-
(**self).try_lazy_query(row_limit, params)
981+
self.stmt.try_lazy_query(row_limit, params)
970982
}
971983

972984
/// A convenience wrapper around `try_lazy_query`.

src/postgres/pool/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ impl InnerConnectionPool {
3030
///
3131
/// It can be shared across tasks.
3232
#[deriving(Clone)]
33-
pub struct PostgresConnectionPool(MutexArc<InnerConnectionPool>);
33+
pub struct PostgresConnectionPool {
34+
priv pool: MutexArc<InnerConnectionPool>
35+
}
3436

3537
impl PostgresConnectionPool {
3638
/// Attempts to create a new pool with the specified number of connections.
@@ -51,7 +53,9 @@ impl PostgresConnectionPool {
5153
}
5254
}
5355

54-
Ok(PostgresConnectionPool(MutexArc::new(pool)))
56+
Ok(PostgresConnectionPool {
57+
pool: MutexArc::new(pool)
58+
})
5559
}
5660

5761
/// A convenience function wrapping `try_new`.
@@ -69,7 +73,7 @@ impl PostgresConnectionPool {
6973
/// If all connections are in use, blocks until one becomes available.
7074
pub fn get_connection(&self) -> PooledPostgresConnection {
7175
let conn = unsafe {
72-
do self.unsafe_access_cond |pool, cvar| {
76+
do self.pool.unsafe_access_cond |pool, cvar| {
7377
while pool.pool.is_empty() {
7478
cvar.wait();
7579
}
@@ -98,7 +102,7 @@ pub struct PooledPostgresConnection {
98102
impl Drop for PooledPostgresConnection {
99103
fn drop(&mut self) {
100104
unsafe {
101-
do self.pool.unsafe_access |pool| {
105+
do self.pool.pool.unsafe_access |pool| {
102106
pool.pool.push(self.conn.take_unwrap());
103107
}
104108
}

0 commit comments

Comments
 (0)