Skip to content

Commit 90c1279

Browse files
committed
Make Connection a tuple struct
1 parent 3832c53 commit 90c1279

File tree

5 files changed

+34
-36
lines changed

5 files changed

+34
-36
lines changed

src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -841,13 +841,11 @@ fn _ensure_send() {
841841
}
842842

843843
/// A connection to a Postgres database.
844-
pub struct Connection {
845-
conn: RefCell<InnerConnection>,
846-
}
844+
pub struct Connection(RefCell<InnerConnection>);
847845

848846
impl fmt::Debug for Connection {
849847
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
850-
let conn = self.conn.borrow();
848+
let conn = self.0.borrow();
851849
fmt.debug_struct("Connection")
852850
.field("stream", &conn.stream.get_ref())
853851
.field("cancel_data", &conn.cancel_data)
@@ -919,7 +917,7 @@ impl Connection {
919917
pub fn connect<T>(params: T, tls: TlsMode) -> result::Result<Connection, ConnectError>
920918
where T: IntoConnectParams
921919
{
922-
InnerConnection::connect(params, tls).map(|conn| Connection { conn: RefCell::new(conn) })
920+
InnerConnection::connect(params, tls).map(|conn| Connection(RefCell::new(conn)))
923921
}
924922

925923
/// Executes a statement, returning the number of rows modified.
@@ -950,7 +948,7 @@ impl Connection {
950948
/// println!("{} rows updated", rows_updated);
951949
/// ```
952950
pub fn execute(&self, query: &str, params: &[&ToSql]) -> Result<u64> {
953-
let (param_types, columns) = try!(self.conn.borrow_mut().raw_prepare("", query));
951+
let (param_types, columns) = try!(self.0.borrow_mut().raw_prepare("", query));
954952
let info = Arc::new(StatementInfo {
955953
name: String::new(),
956954
param_types: param_types,
@@ -986,7 +984,7 @@ impl Connection {
986984
/// }
987985
/// ```
988986
pub fn query<'a>(&'a self, query: &str, params: &[&ToSql]) -> Result<Rows<'a>> {
989-
let (param_types, columns) = try!(self.conn.borrow_mut().raw_prepare("", query));
987+
let (param_types, columns) = try!(self.0.borrow_mut().raw_prepare("", query));
990988
let info = Arc::new(StatementInfo {
991989
name: String::new(),
992990
param_types: param_types,
@@ -1027,7 +1025,7 @@ impl Connection {
10271025

10281026
/// Begins a new transaction with the specified configuration.
10291027
pub fn transaction_with<'a>(&'a self, config: &transaction::Config) -> Result<Transaction<'a>> {
1030-
let mut conn = self.conn.borrow_mut();
1028+
let mut conn = self.0.borrow_mut();
10311029
check_desync!(conn);
10321030
assert!(conn.trans_depth == 0,
10331031
"`transaction` must be called on the active transaction");
@@ -1060,7 +1058,7 @@ impl Connection {
10601058
/// }
10611059
/// ```
10621060
pub fn prepare<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
1063-
self.conn.borrow_mut().prepare(query, self)
1061+
self.0.borrow_mut().prepare(query, self)
10641062
}
10651063

10661064
/// Creates a cached prepared statement.
@@ -1084,14 +1082,14 @@ impl Connection {
10841082
/// }
10851083
/// ```
10861084
pub fn prepare_cached<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
1087-
self.conn.borrow_mut().prepare_cached(query, self)
1085+
self.0.borrow_mut().prepare_cached(query, self)
10881086
}
10891087

10901088
/// Returns the isolation level which will be used for future transactions.
10911089
///
10921090
/// This is a simple wrapper around `SHOW TRANSACTION ISOLATION LEVEL`.
10931091
pub fn transaction_isolation(&self) -> Result<IsolationLevel> {
1094-
let mut conn = self.conn.borrow_mut();
1092+
let mut conn = self.0.borrow_mut();
10951093
check_desync!(conn);
10961094
let result = try!(conn.quick_query("SHOW TRANSACTION ISOLATION LEVEL"));
10971095
IsolationLevel::new(result[0][0].as_ref().unwrap())
@@ -1139,7 +1137,7 @@ impl Connection {
11391137
/// ").unwrap();
11401138
/// ```
11411139
pub fn batch_execute(&self, query: &str) -> Result<()> {
1142-
self.conn.borrow_mut().quick_query(query).map(|_| ())
1140+
self.0.borrow_mut().quick_query(query).map(|_| ())
11431141
}
11441142

11451143
/// Returns a structure providing access to asynchronous notifications.
@@ -1154,18 +1152,18 @@ impl Connection {
11541152
/// Used with the `cancel_query` function. The object returned can be used
11551153
/// to cancel any query executed by the connection it was created from.
11561154
pub fn cancel_data(&self) -> CancelData {
1157-
self.conn.borrow().cancel_data
1155+
self.0.borrow().cancel_data
11581156
}
11591157

11601158
/// Returns the value of the specified Postgres backend parameter, such as
11611159
/// `timezone` or `server_version`.
11621160
pub fn parameter(&self, param: &str) -> Option<String> {
1163-
self.conn.borrow().parameters.get(param).cloned()
1161+
self.0.borrow().parameters.get(param).cloned()
11641162
}
11651163

11661164
/// Sets the notice handler for the connection, returning the old handler.
11671165
pub fn set_notice_handler(&self, handler: Box<HandleNotice>) -> Box<HandleNotice> {
1168-
self.conn.borrow_mut().set_notice_handler(handler)
1166+
self.0.borrow_mut().set_notice_handler(handler)
11691167
}
11701168

11711169
/// Returns whether or not the stream has been desynchronized due to an
@@ -1174,7 +1172,7 @@ impl Connection {
11741172
/// If this has occurred, all further queries will immediately return an
11751173
/// error.
11761174
pub fn is_desynchronized(&self) -> bool {
1177-
self.conn.borrow().is_desynchronized()
1175+
self.0.borrow().is_desynchronized()
11781176
}
11791177

11801178
/// Determines if the `Connection` is currently "active", that is, if there
@@ -1183,15 +1181,15 @@ impl Connection {
11831181
/// The `transaction` method can only be called on the active `Connection`
11841182
/// or `Transaction`.
11851183
pub fn is_active(&self) -> bool {
1186-
self.conn.borrow().trans_depth == 0
1184+
self.0.borrow().trans_depth == 0
11871185
}
11881186

11891187
/// Consumes the connection, closing it.
11901188
///
11911189
/// Functionally equivalent to the `Drop` implementation for `Connection`
11921190
/// except that it returns any error encountered to the caller.
11931191
pub fn finish(self) -> Result<()> {
1194-
let mut conn = self.conn.borrow_mut();
1192+
let mut conn = self.0.borrow_mut();
11951193
conn.finished = true;
11961194
conn.finish_inner()
11971195
}

src/notification.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'a> fmt::Debug for Notifications<'a> {
3535
impl<'conn> Notifications<'conn> {
3636
/// Returns the number of pending notifications.
3737
pub fn len(&self) -> usize {
38-
self.conn.conn.borrow().notifications.len()
38+
self.conn.0.borrow().notifications.len()
3939
}
4040

4141
/// Determines if there are any pending notifications.
@@ -102,7 +102,7 @@ impl<'a> FallibleIterator for Iter<'a> {
102102
type Error = Error;
103103

104104
fn next(&mut self) -> Result<Option<Notification>> {
105-
let mut conn = self.conn.conn.borrow_mut();
105+
let mut conn = self.conn.0.borrow_mut();
106106

107107
if let Some(notification) = conn.notifications.pop_front() {
108108
return Ok(Some(notification));
@@ -127,7 +127,7 @@ impl<'a> FallibleIterator for Iter<'a> {
127127
}
128128

129129
fn size_hint(&self) -> (usize, Option<usize>) {
130-
(self.conn.conn.borrow().notifications.len(), None)
130+
(self.conn.0.borrow().notifications.len(), None)
131131
}
132132
}
133133

@@ -141,7 +141,7 @@ impl<'a> FallibleIterator for BlockingIter<'a> {
141141
type Error = Error;
142142

143143
fn next(&mut self) -> Result<Option<Notification>> {
144-
let mut conn = self.conn.conn.borrow_mut();
144+
let mut conn = self.conn.0.borrow_mut();
145145

146146
if let Some(notification) = conn.notifications.pop_front() {
147147
return Ok(Some(notification));
@@ -177,7 +177,7 @@ impl<'a> FallibleIterator for TimeoutIter<'a> {
177177
type Error = Error;
178178

179179
fn next(&mut self) -> Result<Option<Notification>> {
180-
let mut conn = self.conn.conn.borrow_mut();
180+
let mut conn = self.conn.0.borrow_mut();
181181

182182
if let Some(notification) = conn.notifications.pop_front() {
183183
return Ok(Some(notification));
@@ -202,6 +202,6 @@ impl<'a> FallibleIterator for TimeoutIter<'a> {
202202
}
203203

204204
fn size_hint(&self) -> (usize, Option<usize>) {
205-
(self.conn.conn.borrow().notifications.len(), None)
205+
(self.conn.0.borrow().notifications.len(), None)
206206
}
207207
}

src/rows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'a> Row<'a> {
236236
if !<T as FromSql>::accepts(ty) {
237237
return Some(Err(Error::Conversion(Box::new(WrongType::new(ty.clone())))));
238238
}
239-
let conn = self.stmt.conn().conn.borrow();
239+
let conn = self.stmt.conn().0.borrow();
240240
let value = match self.data[idx] {
241241
Some(ref data) => {
242242
FromSql::from_sql(ty, data, &SessionInfo::new(&conn.parameters))
@@ -346,13 +346,13 @@ impl<'a, 'b> fmt::Debug for LazyRows<'a, 'b> {
346346

347347
impl<'trans, 'stmt> LazyRows<'trans, 'stmt> {
348348
fn finish_inner(&mut self) -> Result<()> {
349-
let mut conn = self.stmt.conn().conn.borrow_mut();
349+
let mut conn = self.stmt.conn().0.borrow_mut();
350350
check_desync!(conn);
351351
conn.close_statement(&self.name, b'P')
352352
}
353353

354354
fn execute(&mut self) -> Result<()> {
355-
let mut conn = self.stmt.conn().conn.borrow_mut();
355+
let mut conn = self.stmt.conn().0.borrow_mut();
356356

357357
try!(conn.stream.write_message(|buf| frontend::execute(&self.name, self.row_limit, buf)));
358358
try!(conn.stream.write_message(|buf| Ok::<(), io::Error>(frontend::sync(buf))));

src/stmt.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'conn> Statement<'conn> {
6969
Ok(())
7070
} else {
7171
self.finished = true;
72-
let mut conn = self.conn.conn.borrow_mut();
72+
let mut conn = self.conn.0.borrow_mut();
7373
check_desync!(conn);
7474
conn.close_statement(&self.info.name, b'S')
7575
}
@@ -81,7 +81,7 @@ impl<'conn> Statement<'conn> {
8181
row_limit: i32,
8282
params: &[&ToSql])
8383
-> Result<(VecDeque<Vec<Option<Vec<u8>>>>, bool)> {
84-
let mut conn = self.conn.conn.borrow_mut();
84+
let mut conn = self.conn.0.borrow_mut();
8585

8686
try!(conn.raw_execute(&self.info.name,
8787
portal_name,
@@ -125,7 +125,7 @@ impl<'conn> Statement<'conn> {
125125
/// println!("{} rows updated", rows_updated);
126126
/// ```
127127
pub fn execute(&self, params: &[&ToSql]) -> Result<u64> {
128-
let mut conn = self.conn.conn.borrow_mut();
128+
let mut conn = self.conn.0.borrow_mut();
129129
check_desync!(conn);
130130
try!(conn.raw_execute(&self.info.name, "", 0, self.param_types(), params));
131131

@@ -228,7 +228,7 @@ impl<'conn> Statement<'conn> {
228228
assert!(self.conn as *const _ == trans.conn() as *const _,
229229
"the `Transaction` passed to `lazy_query` must be associated with the same \
230230
`Connection` as the `Statement`");
231-
let conn = self.conn.conn.borrow();
231+
let conn = self.conn.0.borrow();
232232
check_desync!(conn);
233233
assert!(conn.trans_depth == trans.depth(),
234234
"`lazy_query` must be passed the active transaction");
@@ -265,7 +265,7 @@ impl<'conn> Statement<'conn> {
265265
/// stmt.copy_in(&[], &mut "1\tjohn\n2\tjane\n".as_bytes()).unwrap();
266266
/// ```
267267
pub fn copy_in<R: ReadWithInfo>(&self, params: &[&ToSql], r: &mut R) -> Result<u64> {
268-
let mut conn = self.conn.conn.borrow_mut();
268+
let mut conn = self.conn.0.borrow_mut();
269269
try!(conn.raw_execute(&self.info.name, "", 0, self.param_types(), params));
270270

271271
let (format, column_formats) = match try!(conn.read_message()) {
@@ -368,7 +368,7 @@ impl<'conn> Statement<'conn> {
368368
/// assert_eq!(buf, b"1\tjohn\n2\tjane\n");
369369
/// ```
370370
pub fn copy_out<'a, W: WriteWithInfo>(&'a self, params: &[&ToSql], w: &mut W) -> Result<u64> {
371-
let mut conn = self.conn.conn.borrow_mut();
371+
let mut conn = self.conn.0.borrow_mut();
372372
try!(conn.raw_execute(&self.info.name, "", 0, self.param_types(), params));
373373

374374
let (format, column_formats) = match try!(conn.read_message()) {

src/transaction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'conn> TransactionInternals<'conn> for Transaction<'conn> {
194194

195195
impl<'conn> Transaction<'conn> {
196196
fn finish_inner(&mut self) -> Result<()> {
197-
let mut conn = self.conn.conn.borrow_mut();
197+
let mut conn = self.conn.0.borrow_mut();
198198
debug_assert!(self.depth == conn.trans_depth);
199199
conn.trans_depth -= 1;
200200
match (self.commit.get(), &self.savepoint_name) {
@@ -254,7 +254,7 @@ impl<'conn> Transaction<'conn> {
254254
///
255255
/// Panics if there is an active nested transaction.
256256
pub fn savepoint<'a>(&'a self, name: &str) -> Result<Transaction<'a>> {
257-
let mut conn = self.conn.conn.borrow_mut();
257+
let mut conn = self.conn.0.borrow_mut();
258258
check_desync!(conn);
259259
assert!(conn.trans_depth == self.depth,
260260
"`savepoint` may only be called on the active transaction");
@@ -276,7 +276,7 @@ impl<'conn> Transaction<'conn> {
276276

277277
/// Like `Connection::is_active`.
278278
pub fn is_active(&self) -> bool {
279-
self.conn.conn.borrow().trans_depth == self.depth
279+
self.conn.0.borrow().trans_depth == self.depth
280280
}
281281

282282
/// Alters the configuration of the active transaction.

0 commit comments

Comments
 (0)