Skip to content

Commit d9b7d3c

Browse files
committed
Remove some failure
Avoid reborrowing a RefCell over and over since it'll bloat at least the IR and maybe the resulting binary with failure cruft.
1 parent c40fc64 commit d9b7d3c

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

src/lib.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ impl InnerPostgresConnection {
629629
ReadyForQuery { .. } => break,
630630
DataRow { row } => {
631631
result.push(row.move_iter().map(|opt| {
632-
opt.map(|b| String::from_utf8(b).unwrap())
632+
opt.map(|b| String::from_utf8_lossy(b.as_slice()).into_string())
633633
}).collect());
634634
}
635635
ErrorResponse { fields } => {
@@ -783,12 +783,13 @@ impl PostgresConnection {
783783
/// # }
784784
/// ```
785785
pub fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
786-
check_desync!(self);
787-
if self.conn.borrow().trans_depth != 0 {
786+
let mut conn = self.conn.borrow_mut();
787+
check_desync!(conn);
788+
if conn.trans_depth != 0 {
788789
return Err(PgWrongTransaction);
789790
}
790-
try!(self.quick_query("BEGIN"));
791-
self.conn.borrow_mut().trans_depth += 1;
791+
try!(conn.quick_query("BEGIN"));
792+
conn.trans_depth += 1;
792793
Ok(PostgresTransaction {
793794
conn: self,
794795
commit: Cell::new(false),
@@ -882,10 +883,6 @@ impl PostgresConnection {
882883
self.conn.borrow().canary()
883884
}
884885

885-
fn quick_query(&self, query: &str) -> PostgresResult<Vec<Vec<Option<String>>>> {
886-
self.conn.borrow_mut().quick_query(query)
887-
}
888-
889886
fn wait_for_ready(&self) -> PostgresResult<()> {
890887
self.conn.borrow_mut().wait_for_ready()
891888
}
@@ -930,23 +927,25 @@ impl<'conn> Drop for PostgresTransaction<'conn> {
930927

931928
impl<'conn> PostgresTransaction<'conn> {
932929
fn finish_inner(&mut self) -> PostgresResult<()> {
933-
debug_assert!(self.depth == self.conn.conn.borrow().trans_depth);
930+
let mut conn = self.conn.conn.borrow_mut();
931+
debug_assert!(self.depth == conn.trans_depth);
934932
let query = match (self.commit.get(), self.depth != 1) {
935933
(false, true) => "ROLLBACK TO sp",
936934
(false, false) => "ROLLBACK",
937935
(true, true) => "RELEASE sp",
938936
(true, false) => "COMMIT",
939937
};
940-
self.conn.conn.borrow_mut().trans_depth -= 1;
941-
self.conn.quick_query(query).map(|_| ())
938+
conn.trans_depth -= 1;
939+
conn.quick_query(query).map(|_| ())
942940
}
943941

944942
/// Like `PostgresConnection::prepare`.
945943
pub fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
946-
if self.conn.conn.borrow().trans_depth != self.depth {
944+
let mut conn = self.conn.conn.borrow_mut();
945+
if conn.trans_depth != self.depth {
947946
return Err(PgWrongTransaction);
948947
}
949-
self.conn.conn.borrow_mut().prepare(query, self.conn)
948+
conn.prepare(query, self.conn)
950949
}
951950

952951
/// Like `PostgresConnection::execute`.
@@ -965,12 +964,13 @@ impl<'conn> PostgresTransaction<'conn> {
965964

966965
/// Like `PostgresConnection::transaction`.
967966
pub fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
968-
check_desync!(self.conn);
969-
if self.conn.conn.borrow().trans_depth != self.depth {
967+
let mut conn = self.conn.conn.borrow_mut();
968+
check_desync!(conn);
969+
if conn.trans_depth != self.depth {
970970
return Err(PgWrongTransaction);
971971
}
972-
try!(self.conn.quick_query("SAVEPOINT sp"));
973-
self.conn.conn.borrow_mut().trans_depth += 1;
972+
try!(conn.quick_query("SAVEPOINT sp"));
973+
conn.trans_depth += 1;
974974
Ok(PostgresTransaction {
975975
conn: self.conn,
976976
commit: Cell::new(false),
@@ -1077,6 +1077,7 @@ impl<'conn> PostgresStatement<'conn> {
10771077

10781078
fn inner_execute(&self, portal_name: &str, row_limit: i32, params: &[&ToSql])
10791079
-> PostgresResult<()> {
1080+
let mut conn = self.conn.conn.borrow_mut();
10801081
if self.param_types.len() != params.len() {
10811082
return Err(PgWrongParamCount {
10821083
expected: self.param_types.len(),
@@ -1093,7 +1094,7 @@ impl<'conn> PostgresStatement<'conn> {
10931094

10941095
let result_formats = Vec::from_elem(self.result_desc.len(), Binary as i16);
10951096

1096-
try_pg!(self.conn.write_messages([
1097+
try_pg!(conn.write_messages([
10971098
Bind {
10981099
portal: portal_name,
10991100
statement: self.name.as_slice(),
@@ -1107,14 +1108,14 @@ impl<'conn> PostgresStatement<'conn> {
11071108
},
11081109
Sync]));
11091110

1110-
match try_pg!(self.conn.read_message()) {
1111+
match try_pg!(conn.read_message()) {
11111112
BindComplete => Ok(()),
11121113
ErrorResponse { fields } => {
11131114
try!(self.conn.wait_for_ready());
11141115
Err(PgDbError(PostgresDbError::new(fields)))
11151116
}
11161117
_ => {
1117-
self.conn.conn.borrow_mut().desynchronized = true;
1118+
conn.desynchronized = true;
11181119
return Err(PgBadResponse);
11191120
}
11201121
}
@@ -1171,12 +1172,13 @@ impl<'conn> PostgresStatement<'conn> {
11711172
check_desync!(self.conn);
11721173
try!(self.inner_execute("", 0, params));
11731174

1175+
let mut conn = self.conn.conn.borrow_mut();
11741176
let num;
11751177
loop {
1176-
match try_pg!(self.conn.read_message()) {
1178+
match try_pg!(conn.read_message()) {
11771179
DataRow { .. } => {}
11781180
ErrorResponse { fields } => {
1179-
try!(self.conn.wait_for_ready());
1181+
try!(conn.wait_for_ready());
11801182
return Err(PgDbError(PostgresDbError::new(fields)));
11811183
}
11821184
CommandComplete { tag } => {
@@ -1189,12 +1191,12 @@ impl<'conn> PostgresStatement<'conn> {
11891191
break;
11901192
}
11911193
_ => {
1192-
self.conn.conn.borrow_mut().desynchronized = true;
1194+
conn.desynchronized = true;
11931195
return Err(PgBadResponse);
11941196
}
11951197
}
11961198
}
1197-
try!(self.conn.wait_for_ready());
1199+
try!(conn.wait_for_ready());
11981200

11991201
Ok(num)
12001202
}
@@ -1263,29 +1265,33 @@ impl<'stmt> Drop for PostgresRows<'stmt> {
12631265

12641266
impl<'stmt> PostgresRows<'stmt> {
12651267
fn finish_inner(&mut self) -> PostgresResult<()> {
1266-
check_desync!(self.stmt.conn);
1267-
try_pg!(self.stmt.conn.write_messages([
1268+
let mut conn = self.stmt.conn.conn.borrow_mut();
1269+
check_desync!(conn);
1270+
try_pg!(conn.write_messages([
12681271
Close {
12691272
variant: b'P',
12701273
name: self.name.as_slice()
12711274
},
12721275
Sync]));
1276+
12731277
loop {
1274-
match try_pg!(self.stmt.conn.read_message()) {
1278+
match try_pg!(conn.read_message()) {
12751279
ReadyForQuery { .. } => break,
12761280
ErrorResponse { fields } => {
1277-
try!(self.stmt.conn.wait_for_ready());
1281+
try!(conn.wait_for_ready());
12781282
return Err(PgDbError(PostgresDbError::new(fields)));
12791283
}
12801284
_ => {}
12811285
}
12821286
}
1287+
12831288
Ok(())
12841289
}
12851290

12861291
fn read_rows(&mut self) -> PostgresResult<()> {
1292+
let mut conn = self.stmt.conn.conn.borrow_mut();
12871293
loop {
1288-
match try_pg!(self.stmt.conn.read_message()) {
1294+
match try_pg!(conn.read_message()) {
12891295
EmptyQueryResponse | CommandComplete { .. } => {
12901296
self.more_rows = false;
12911297
break;
@@ -1296,16 +1302,16 @@ impl<'stmt> PostgresRows<'stmt> {
12961302
},
12971303
DataRow { row } => self.data.push(row),
12981304
ErrorResponse { fields } => {
1299-
try!(self.stmt.conn.wait_for_ready());
1305+
try!(conn.wait_for_ready());
13001306
return Err(PgDbError(PostgresDbError::new(fields)));
13011307
}
13021308
_ => {
1303-
self.stmt.conn.conn.borrow_mut().desynchronized = true;
1309+
conn.desynchronized = true;
13041310
return Err(PgBadResponse);
13051311
}
13061312
}
13071313
}
1308-
self.stmt.conn.wait_for_ready()
1314+
conn.wait_for_ready()
13091315
}
13101316

13111317
fn execute(&mut self) -> PostgresResult<()> {

0 commit comments

Comments
 (0)