Skip to content

Commit c543963

Browse files
committed
Move from Cell to RefCell
1 parent a86d33c commit c543963

1 file changed

Lines changed: 22 additions & 29 deletions

File tree

lib.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use extra::container::Deque;
7575
use extra::ringbuf::RingBuf;
7676
use extra::url::{UserInfo, Url};
7777
use ssl::{SslStream, SslContext};
78-
use std::cell::Cell;
78+
use std::cell::RefCell;
7979
use std::io::io_error;
8080
use std::io::buffered::BufferedStream;
8181
use std::io::net;
@@ -184,7 +184,7 @@ impl<'self> Iterator<PostgresNotification> for
184184
/// `next` may return `Some` notification after returning `None` if a new
185185
/// notification was received.
186186
fn next(&mut self) -> Option<PostgresNotification> {
187-
do self.conn.conn.with_mut_ref |conn| {
187+
do self.conn.conn.with_mut |conn| {
188188
conn.notifications.pop_front()
189189
}
190190
}
@@ -535,7 +535,7 @@ impl InnerPostgresConnection {
535535
name: stmt_name,
536536
param_types: param_types,
537537
result_desc: result_desc,
538-
next_portal_id: Cell::new(0)
538+
next_portal_id: RefCell::new(0)
539539
})
540540
}
541541

@@ -549,7 +549,7 @@ impl InnerPostgresConnection {
549549

550550
/// A connection to a Postgres database.
551551
pub struct PostgresConnection {
552-
priv conn: Cell<InnerPostgresConnection>
552+
priv conn: RefCell<InnerPostgresConnection>
553553
}
554554

555555
impl PostgresConnection {
@@ -568,7 +568,7 @@ impl PostgresConnection {
568568
-> Result<PostgresConnection, PostgresConnectError> {
569569
do InnerPostgresConnection::try_connect(url, ssl).map |conn| {
570570
PostgresConnection {
571-
conn: Cell::new(conn)
571+
conn: RefCell::new(conn)
572572
}
573573
}
574574
}
@@ -588,10 +588,8 @@ impl PostgresConnection {
588588
/// Sets the notice handler for the connection, returning the old handler.
589589
pub fn set_notice_handler(&self, handler: ~PostgresNoticeHandler)
590590
-> ~PostgresNoticeHandler {
591-
let mut conn = self.conn.take();
592-
let handler = conn.set_notice_handler(handler);
593-
self.conn.put_back(conn);
594-
handler
591+
let mut conn = self.conn.borrow_mut();
592+
conn.get().set_notice_handler(handler)
595593
}
596594

597595
/// Returns an iterator over asynchronous notification messages.
@@ -613,7 +611,7 @@ impl PostgresConnection {
613611
/// not outlive that connection.
614612
pub fn try_prepare<'a>(&'a self, query: &str)
615613
-> Result<NormalPostgresStatement<'a>, PostgresDbError> {
616-
do self.conn.with_mut_ref |conn| {
614+
do self.conn.with_mut |conn| {
617615
conn.try_prepare(query, self)
618616
}
619617
}
@@ -642,7 +640,7 @@ impl PostgresConnection {
642640
self.quick_query("BEGIN");
643641
PostgresTransaction {
644642
conn: self,
645-
commit: Cell::new(true),
643+
commit: RefCell::new(true),
646644
nested: false
647645
}
648646
}
@@ -678,13 +676,13 @@ impl PostgresConnection {
678676
/// Used with the `cancel_query` function. The object returned can be used
679677
/// to cancel any query executed by the connection it was created from.
680678
pub fn cancel_data(&self) -> PostgresCancelData {
681-
do self.conn.with_ref |conn| {
679+
do self.conn.with |conn| {
682680
conn.cancel_data
683681
}
684682
}
685683

686684
fn quick_query(&self, query: &str) {
687-
do self.conn.with_mut_ref |conn| {
685+
do self.conn.with_mut |conn| {
688686
conn.write_messages([Query { query: query }]);
689687

690688
loop {
@@ -700,19 +698,19 @@ impl PostgresConnection {
700698
}
701699

702700
fn wait_for_ready(&self) {
703-
do self.conn.with_mut_ref |conn| {
701+
do self.conn.with_mut |conn| {
704702
conn.wait_for_ready()
705703
}
706704
}
707705

708706
fn read_message(&self) -> BackendMessage {
709-
do self.conn.with_mut_ref |conn| {
707+
do self.conn.with_mut |conn| {
710708
conn.read_message()
711709
}
712710
}
713711

714712
fn write_messages(&self, messages: &[FrontendMessage]) {
715-
do self.conn.with_mut_ref |conn| {
713+
do self.conn.with_mut |conn| {
716714
conn.write_messages(messages)
717715
}
718716
}
@@ -731,15 +729,15 @@ pub enum SslMode {
731729
/// Represents a transaction on a database connection
732730
pub struct PostgresTransaction<'self> {
733731
priv conn: &'self PostgresConnection,
734-
priv commit: Cell<bool>,
732+
priv commit: RefCell<bool>,
735733
priv nested: bool
736734
}
737735

738736
#[unsafe_destructor]
739737
impl<'self> Drop for PostgresTransaction<'self> {
740738
fn drop(&mut self) {
741739
do io_error::cond.trap(|_| {}).inside {
742-
if task::failing() || !self.commit.take() {
740+
if task::failing() || !self.commit.with(|x| *x) {
743741
if self.nested {
744742
self.conn.quick_query("ROLLBACK TO sp");
745743
} else {
@@ -791,7 +789,7 @@ impl<'self> PostgresTransaction<'self> {
791789
self.conn.quick_query("SAVEPOINT sp");
792790
PostgresTransaction {
793791
conn: self.conn,
794-
commit: Cell::new(true),
792+
commit: RefCell::new(true),
795793
nested: true
796794
}
797795
}
@@ -803,21 +801,17 @@ impl<'self> PostgresTransaction<'self> {
803801

804802
/// Determines if the transaction is currently set to commit or roll back.
805803
pub fn will_commit(&self) -> bool {
806-
let commit = self.commit.take();
807-
self.commit.put_back(commit);
808-
commit
804+
self.commit.with(|x| *x)
809805
}
810806

811807
/// Sets the transaction to commit at its completion.
812808
pub fn set_commit(&self) {
813-
self.commit.take();
814-
self.commit.put_back(true);
809+
self.commit.with_mut(|x| *x = true);
815810
}
816811

817812
/// Sets the transaction to roll back at its completion.
818813
pub fn set_rollback(&self) {
819-
self.commit.take();
820-
self.commit.put_back(false);
814+
self.commit.with_mut(|x| *x = false);
821815
}
822816
}
823817

@@ -881,7 +875,7 @@ pub struct NormalPostgresStatement<'self> {
881875
priv name: ~str,
882876
priv param_types: ~[PostgresType],
883877
priv result_desc: ~[ResultDescription],
884-
priv next_portal_id: Cell<uint>
878+
priv next_portal_id: RefCell<uint>
885879
}
886880

887881
#[unsafe_destructor]
@@ -948,9 +942,8 @@ impl<'self> NormalPostgresStatement<'self> {
948942

949943
fn try_lazy_query<'a>(&'a self, row_limit: uint, params: &[&ToSql])
950944
-> Result<PostgresResult<'a>, PostgresDbError> {
951-
let id = self.next_portal_id.take();
945+
let id = self.next_portal_id.with_mut(|x| { *x += 1; *x - 1 });
952946
let portal_name = format!("{}_portal_{}", self.name, id);
953-
self.next_portal_id.put_back(id + 1);
954947

955948
match self.execute(portal_name, row_limit, params) {
956949
Some(err) => {

0 commit comments

Comments
 (0)