Skip to content

Commit 4c91a68

Browse files
committed
try! -> ?
1 parent 73e4ec1 commit 4c91a68

31 files changed

+332
-336
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ fn main() {
6363
### Connecting
6464
Connect to a Postgres server using the standard URI format:
6565
```rust
66-
let conn = try!(Connection::connect("postgres://user:pass@host:port/database?arg1=val1&arg2=val2",
67-
TlsMode::None));
66+
let conn = Connection::connect("postgres://user:pass@host:port/database?arg1=val1&arg2=val2",
67+
TlsMode::None)?;
6868
```
6969
`pass` may be omitted if not needed. `port` defaults to `5432` and `database`
7070
defaults to the value of `user` if not specified. The driver supports `trust`,
@@ -75,7 +75,7 @@ be set to the absolute path to the directory containing the socket file. Since
7575
`/` is a reserved character in URLs, the path should be URL encoded. If Postgres
7676
stored its socket files in `/run/postgres`, the connection would then look like:
7777
```rust
78-
let conn = try!(Connection::connect("postgres://postgres@%2Frun%2Fpostgres", TlsMode::None));
78+
let conn = Connection::connect("postgres://postgres@%2Frun%2Fpostgres", TlsMode::None)?;
7979
```
8080
Paths which contain non-UTF8 characters can be handled in a different manner;
8181
see the documentation for details.
@@ -89,7 +89,7 @@ that query parameters are 1-indexed rather than the more common 0-indexing.
8989
`execute` returns the number of rows affected by the query (or 0 if not
9090
applicable):
9191
```rust
92-
let updates = try!(conn.execute("UPDATE foo SET bar = $1 WHERE baz = $2", &[&1i32, &"biz"]));
92+
let updates = conn.execute("UPDATE foo SET bar = $1 WHERE baz = $2", &[&1i32, &"biz"])?;
9393
println!("{} rows were updated", updates);
9494
```
9595

@@ -98,7 +98,7 @@ The fields in a row can be accessed either by their indices or their column
9898
names, though access by index is more efficient. Unlike statement parameters,
9999
result columns are zero-indexed.
100100
```rust
101-
for row in &try!(conn.query("SELECT bar, baz FROM foo WHERE buz = $1", &[&1i32])) {
101+
for row in &conn.query("SELECT bar, baz FROM foo WHERE buz = $1", &[&1i32])? {
102102
let bar: i32 = row.get(0);
103103
let baz: String = row.get("baz");
104104
println!("bar: {}, baz: {}", bar, baz);
@@ -110,9 +110,9 @@ If the same statement will be executed repeatedly (possibly with different
110110
parameters), explicitly preparing it can improve performance:
111111

112112
```rust
113-
let stmt = try!(conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2"));
113+
let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2")?;
114114
for (bar, baz) in updates {
115-
try!(stmt.execute(&[bar, baz]));
115+
stmt.execute(&[bar, baz])?;
116116
}
117117
```
118118

@@ -122,13 +122,13 @@ The `transaction` method will start a new transaction. It returns a
122122
`Connection` as well as methods to control the result of the
123123
transaction:
124124
```rust
125-
let trans = try!(conn.transaction());
125+
let trans = conn.transaction()?;
126126

127-
try!(trans.execute(...));
128-
let stmt = try!(trans.prepare(...));
127+
trans.execute(...)?;
128+
let stmt = trans.prepare(...)?;
129129
// ...
130130

131-
try!(trans.commit());
131+
trans.commit()?;
132132
```
133133
The transaction will be active until the `Transaction` object falls out of
134134
scope. A transaction will roll back by default. Nested transactions are

codegen/src/type_gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ r#"impl fmt::Display for Type {{
159159
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {{
160160
match self.schema() {{
161161
"public" | "pg_catalog" => {{}}
162-
schema => try!(write!(fmt, "{{}}.", schema)),
162+
schema => write!(fmt, "{{}}.", schema)?,
163163
}}
164164
fmt.write_str(self.name())
165165
}}

postgres-shared/src/error/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,24 @@ impl DbError {
159159
let mut line = None;
160160
let mut routine = None;
161161

162-
while let Some(field) = try!(fields.next()) {
162+
while let Some(field) = fields.next()? {
163163
match field.type_() {
164164
b'S' => severity = Some(field.value().to_owned()),
165165
b'C' => code = Some(SqlState::from_code(field.value())),
166166
b'M' => message = Some(field.value().to_owned()),
167167
b'D' => detail = Some(field.value().to_owned()),
168168
b'H' => hint = Some(field.value().to_owned()),
169169
b'P' => {
170-
normal_position = Some(try!(field.value().parse::<u32>().map_err(|_| {
170+
normal_position = Some(field.value().parse::<u32>().map_err(|_| {
171171
io::Error::new(io::ErrorKind::InvalidInput,
172172
"`P` field did not contain an integer")
173-
})));
173+
})?);
174174
}
175175
b'p' => {
176-
internal_position = Some(try!(field.value().parse::<u32>().map_err(|_| {
176+
internal_position = Some(field.value().parse::<u32>().map_err(|_| {
177177
io::Error::new(io::ErrorKind::InvalidInput,
178178
"`p` field did not contain an integer")
179-
})));
179+
})?);
180180
}
181181
b'q' => internal_query = Some(field.value().to_owned()),
182182
b'W' => where_ = Some(field.value().to_owned()),
@@ -187,31 +187,31 @@ impl DbError {
187187
b'n' => constraint = Some(field.value().to_owned()),
188188
b'F' => file = Some(field.value().to_owned()),
189189
b'L' => {
190-
line = Some(try!(field.value().parse::<u32>().map_err(|_| {
190+
line = Some(field.value().parse::<u32>().map_err(|_| {
191191
io::Error::new(io::ErrorKind::InvalidInput,
192192
"`L` field did not contain an integer")
193-
})));
193+
})?);
194194
}
195195
b'R' => routine = Some(field.value().to_owned()),
196196
b'V' => {
197-
parsed_severity = Some(try!(Severity::from_str(field.value()).ok_or_else(|| {
197+
parsed_severity = Some(Severity::from_str(field.value()).ok_or_else(|| {
198198
io::Error::new(io::ErrorKind::InvalidInput,
199199
"`V` field contained an invalid value")
200-
})));
200+
})?);
201201
}
202202
_ => {},
203203
}
204204
}
205205

206206
Ok(DbError {
207-
severity: try!(severity.ok_or_else(|| {
207+
severity: severity.ok_or_else(|| {
208208
io::Error::new(io::ErrorKind::InvalidInput, "`S` field missing")
209-
})),
209+
})?,
210210
parsed_severity: parsed_severity,
211-
code: try!(code.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput,
212-
"`C` field missing"))),
213-
message: try!(message.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput,
214-
"`M` field missing"))),
211+
code: code.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput,
212+
"`C` field missing"))?,
213+
message: message.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput,
214+
"`M` field missing"))?,
215215
detail: detail,
216216
hint: hint,
217217
position: match normal_position {
@@ -221,10 +221,10 @@ impl DbError {
221221
Some(position) => {
222222
Some(ErrorPosition::Internal {
223223
position: position,
224-
query: try!(internal_query.ok_or_else(|| {
224+
query: internal_query.ok_or_else(|| {
225225
io::Error::new(io::ErrorKind::InvalidInput,
226226
"`q` field missing but `p` field present")
227-
})),
227+
})?,
228228
})
229229
}
230230
None => None,
@@ -310,7 +310,7 @@ pub enum ConnectError {
310310

311311
impl fmt::Display for ConnectError {
312312
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
313-
try!(fmt.write_str(error::Error::description(self)));
313+
fmt.write_str(error::Error::description(self))?;
314314
match *self {
315315
ConnectError::ConnectParams(ref msg) => write!(fmt, ": {}", msg),
316316
ConnectError::Db(ref err) => write!(fmt, ": {}", err),

postgres-shared/src/params/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl IntoConnectParams for Url {
7878
fn into_connect_params(self) -> Result<ConnectParams, Box<Error + Sync + Send>> {
7979
let Url { host, port, user, path: url::Path { mut path, query: options, .. }, .. } = self;
8080

81-
let maybe_path = try!(url::decode_component(&host));
81+
let maybe_path = url::decode_component(&host)?;
8282
let target = if maybe_path.starts_with('/') {
8383
ConnectTarget::Unix(PathBuf::from(maybe_path))
8484
} else {

postgres-shared/src/params/url.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ impl Url {
5151

5252
pub fn parse(rawurl: &str) -> DecodeResult<Url> {
5353
// scheme
54-
let (scheme, rest) = try!(get_scheme(rawurl));
54+
let (scheme, rest) = get_scheme(rawurl)?;
5555

5656
// authority
57-
let (userinfo, host, port, rest) = try!(get_authority(rest));
57+
let (userinfo, host, port, rest) = get_authority(rest)?;
5858

5959
// path
6060
let has_authority = !host.is_empty();
61-
let (path, rest) = try!(get_path(rest, has_authority));
61+
let (path, rest) = get_path(rest, has_authority)?;
6262

6363
// query and fragment
64-
let (query, fragment) = try!(get_query_fragment(rest));
64+
let (query, fragment) = get_query_fragment(rest)?;
6565

6666
let url = Url::new(scheme.to_owned(),
6767
userinfo,
@@ -84,10 +84,10 @@ impl Path {
8484
}
8585

8686
pub fn parse(rawpath: &str) -> DecodeResult<Path> {
87-
let (path, rest) = try!(get_path(rawpath, false));
87+
let (path, rest) = get_path(rawpath, false)?;
8888

8989
// query and fragment
90-
let (query, fragment) = try!(get_query_fragment(&rest));
90+
let (query, fragment) = get_query_fragment(&rest)?;
9191

9292
Ok(Path {
9393
path: path,
@@ -177,7 +177,7 @@ fn query_from_str(rawquery: &str) -> DecodeResult<Query> {
177177
if !rawquery.is_empty() {
178178
for p in rawquery.split('&') {
179179
let (k, v) = split_char_first(p, '=');
180-
query.push((try!(decode_component(k)), try!(decode_component(v))));
180+
query.push((decode_component(k)?, decode_component(v)?));
181181
}
182182
}
183183

@@ -316,13 +316,13 @@ fn get_authority(rawurl: &str) -> DecodeResult<(Option<UserInfo>, &str, Option<u
316316
colon_count = 0; // reset count
317317
match st {
318318
State::Start => {
319-
let user = try!(decode_component(&rawurl[begin..i]));
319+
let user = decode_component(&rawurl[begin..i])?;
320320
userinfo = Some(UserInfo::new(user, None));
321321
st = State::InHost;
322322
}
323323
State::PassHostPort => {
324-
let user = try!(decode_component(&rawurl[begin..pos]));
325-
let pass = try!(decode_component(&rawurl[pos + 1..i]));
324+
let user = decode_component(&rawurl[begin..pos])?;
325+
let pass = decode_component(&rawurl[pos + 1..i])?;
326326
userinfo = Some(UserInfo::new(user, Some(pass)));
327327
st = State::InHost;
328328
}
@@ -392,7 +392,7 @@ fn get_path(rawurl: &str, is_authority: bool) -> DecodeResult<(String, &str)> {
392392
if is_authority && end != 0 && !rawurl.starts_with('/') {
393393
Err("Non-empty path must begin with '/' in presence of authority.".to_owned())
394394
} else {
395-
Ok((try!(decode_component(&rawurl[0..end])), &rawurl[end..len]))
395+
Ok((decode_component(&rawurl[0..end])?, &rawurl[end..len]))
396396
}
397397
}
398398

@@ -403,11 +403,11 @@ fn get_query_fragment(rawurl: &str) -> DecodeResult<(Query, Option<String>)> {
403403
// Parse the fragment if available
404404
let fragment = match raw_fragment {
405405
"" => None,
406-
raw => Some(try!(decode_component(raw))),
406+
raw => Some(decode_component(raw)?),
407407
};
408408

409409
match before_fragment.chars().next() {
410-
Some('?') => Ok((try!(query_from_str(&before_fragment[1..])), fragment)),
410+
Some('?') => Ok((query_from_str(&before_fragment[1..])?, fragment)),
411411
None => Ok((vec![], fragment)),
412412
_ => Err(format!("Query didn't start with '?': '{}..'", before_fragment)),
413413
}

postgres-shared/src/rows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
6060
indices: Vec::with_capacity(it.size_hint().0),
6161
};
6262

63-
while let Some(cell) = try!(it.next()) {
63+
while let Some(cell) = it.next()? {
6464
let index = match cell {
6565
Some(cell) => {
6666
let base = row.buf.len();

postgres-shared/src/types/bit_vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use types::{FromSql, ToSql, IsNull, Type};
88

99
impl FromSql for BitVec {
1010
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<Error + Sync + Send>> {
11-
let varbit = try!(types::varbit_from_sql(raw));
11+
let varbit = types::varbit_from_sql(raw)?;
1212
let mut bitvec = BitVec::from_bytes(varbit.bytes());
1313
while bitvec.len() > varbit.len() {
1414
bitvec.pop();
@@ -25,7 +25,7 @@ impl ToSql for BitVec {
2525
_: &Type,
2626
mut out: &mut Vec<u8>)
2727
-> Result<IsNull, Box<Error + Sync + Send>> {
28-
try!(types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out));
28+
types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?;
2929
Ok(IsNull::No)
3030
}
3131

postgres-shared/src/types/chrono.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl FromSql for NaiveDateTime {
1515
fn from_sql(_: &Type,
1616
raw: &[u8])
1717
-> Result<NaiveDateTime, Box<Error + Sync + Send>> {
18-
let t = try!(types::timestamp_from_sql(raw));
18+
let t = types::timestamp_from_sql(raw)?;
1919
Ok(base() + Duration::microseconds(t))
2020
}
2121

@@ -43,7 +43,7 @@ impl FromSql for DateTime<UTC> {
4343
fn from_sql(type_: &Type,
4444
raw: &[u8])
4545
-> Result<DateTime<UTC>, Box<Error + Sync + Send>> {
46-
let naive = try!(NaiveDateTime::from_sql(type_, raw));
46+
let naive = NaiveDateTime::from_sql(type_, raw)?;
4747
Ok(DateTime::from_utc(naive, UTC))
4848
}
4949

@@ -66,7 +66,7 @@ impl FromSql for DateTime<Local> {
6666
fn from_sql(type_: &Type,
6767
raw: &[u8])
6868
-> Result<DateTime<Local>, Box<Error + Sync + Send>> {
69-
let utc = try!(DateTime::<UTC>::from_sql(type_, raw));
69+
let utc = DateTime::<UTC>::from_sql(type_, raw)?;
7070
Ok(utc.with_timezone(&Local))
7171
}
7272

@@ -89,7 +89,7 @@ impl FromSql for DateTime<FixedOffset> {
8989
fn from_sql(type_: &Type,
9090
raw: &[u8])
9191
-> Result<DateTime<FixedOffset>, Box<Error + Sync + Send>> {
92-
let utc = try!(DateTime::<UTC>::from_sql(type_, raw));
92+
let utc = DateTime::<UTC>::from_sql(type_, raw)?;
9393
Ok(utc.with_timezone(&FixedOffset::east(0)))
9494
}
9595

@@ -112,7 +112,7 @@ impl FromSql for NaiveDate {
112112
fn from_sql(_: &Type,
113113
raw: &[u8])
114114
-> Result<NaiveDate, Box<Error + Sync + Send>> {
115-
let jd = try!(types::date_from_sql(raw));
115+
let jd = types::date_from_sql(raw)?;
116116
Ok(base().date() + Duration::days(jd as i64))
117117
}
118118

@@ -141,7 +141,7 @@ impl FromSql for NaiveTime {
141141
fn from_sql(_: &Type,
142142
raw: &[u8])
143143
-> Result<NaiveTime, Box<Error + Sync + Send>> {
144-
let usec = try!(types::time_from_sql(raw));
144+
let usec = types::time_from_sql(raw)?;
145145
Ok(NaiveTime::from_hms(0, 0, 0) + Duration::microseconds(usec))
146146
}
147147

postgres-shared/src/types/eui48.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl FromSql for MacAddress {
1010
fn from_sql(_: &Type,
1111
raw: &[u8])
1212
-> Result<MacAddress, Box<Error + Sync + Send>> {
13-
let bytes = try!(types::macaddr_from_sql(raw));
13+
let bytes = types::macaddr_from_sql(raw)?;
1414
Ok(MacAddress::new(bytes))
1515
}
1616

0 commit comments

Comments
 (0)