Skip to content

Commit 774fd93

Browse files
committed
Start using if let
1 parent abd60ef commit 774fd93

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

src/lib.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
//! }
5252
//! ```
5353
#![doc(html_root_url="http://www.rust-ci.org/sfackler/rust-postgres/doc")]
54-
#![feature(macro_rules, struct_variant, phase, unsafe_destructor, slicing_syntax)]
54+
#![feature(macro_rules, struct_variant, phase, unsafe_destructor, slicing_syntax, if_let)]
5555
#![warn(missing_doc)]
5656

5757
extern crate collections;
@@ -382,10 +382,7 @@ impl InnerPostgresConnection {
382382
..
383383
} = params;
384384

385-
let user = match user {
386-
Some(user) => user,
387-
None => return Err(MissingUser),
388-
};
385+
let user = try!(user.ok_or(MissingUser));
389386

390387
let mut conn = InnerPostgresConnection {
391388
stream: BufferedStream::new(stream),
@@ -447,9 +444,8 @@ impl InnerPostgresConnection {
447444
loop {
448445
match try_desync!(self, self.stream.read_message()) {
449446
NoticeResponse { fields } => {
450-
match PostgresDbError::new_raw(fields) {
451-
Ok(err) => self.notice_handler.handle(err),
452-
Err(()) => {}
447+
if let Ok(err) = PostgresDbError::new_raw(fields) {
448+
self.notice_handler.handle(err);
453449
}
454450
}
455451
NotificationResponse { pid, channel, payload } => {
@@ -471,19 +467,13 @@ impl InnerPostgresConnection {
471467
match try_pg_conn!(self.read_message_()) {
472468
AuthenticationOk => return Ok(()),
473469
AuthenticationCleartextPassword => {
474-
let pass = match user.password {
475-
Some(pass) => pass,
476-
None => return Err(MissingPassword)
477-
};
470+
let pass = try!(user.password.ok_or(MissingPassword));
478471
try_pg_conn!(self.write_messages([PasswordMessage {
479472
password: pass[],
480473
}]));
481474
}
482475
AuthenticationMD5Password { salt } => {
483-
let pass = match user.password {
484-
Some(pass) => pass,
485-
None => return Err(MissingPassword)
486-
};
476+
let pass = try!(user.password.ok_or(MissingPassword));
487477
let hasher = Hasher::new(MD5);
488478
hasher.update(pass.as_bytes());
489479
hasher.update(user.user.as_bytes());
@@ -651,9 +641,8 @@ impl InnerPostgresConnection {
651641
}
652642

653643
fn get_type_name(&mut self, oid: Oid) -> PostgresResult<String> {
654-
match self.unknown_types.find(&oid) {
655-
Some(name) => return Ok(name.clone()),
656-
None => {}
644+
if let Some(name) = self.unknown_types.find(&oid) {
645+
return Ok(name.clone());
657646
}
658647
let name = try!(self.quick_query(format!("SELECT typname FROM pg_type \
659648
WHERE oid={}", oid)[]))
@@ -1169,8 +1158,7 @@ impl<'conn> PostgresStatement<'conn> {
11691158
}
11701159
let mut values = vec![];
11711160
for (param, ty) in params.iter().zip(self.param_types.iter()) {
1172-
let value = try!(param.to_sql(ty));
1173-
values.push(value);
1161+
values.push(try!(param.to_sql(ty)));
11741162
};
11751163

11761164
try_pg!(conn.write_messages([
@@ -1427,9 +1415,8 @@ impl<'stmt> PostgresRows<'stmt> {
14271415

14281416
fn try_next(&mut self) -> Option<PostgresResult<PostgresRow<'stmt>>> {
14291417
if self.data.is_empty() && self.more_rows {
1430-
match self.execute() {
1431-
Ok(()) => {}
1432-
Err(err) => return Some(Err(err))
1418+
if let Err(err) = self.execute() {
1419+
return Some(Err(err));
14331420
}
14341421
}
14351422

@@ -1476,10 +1463,7 @@ impl<'stmt> PostgresRow<'stmt> {
14761463
/// Returns an `Error` value if the index does not reference a column or
14771464
/// the return type is not compatible with the Postgres type.
14781465
pub fn get_opt<I, T>(&self, idx: I) -> PostgresResult<T> where I: RowIndex, T: FromSql {
1479-
let idx = match idx.idx(self.stmt) {
1480-
Some(idx) => idx,
1481-
None => return Err(PgInvalidColumn)
1482-
};
1466+
let idx = try!(idx.idx(self.stmt).ok_or(PgInvalidColumn));
14831467
FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx])
14841468
}
14851469

0 commit comments

Comments
 (0)