Skip to content

Commit 2151637

Browse files
committed
Use the try operator
1 parent bdac366 commit 2151637

File tree

11 files changed

+370
-380
lines changed

11 files changed

+370
-380
lines changed

rustfmt.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
write_mode = "Overwrite"
2-
2+
use_try_shorthand = true

src/abbrev.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Abbreviations {
137137
let mut abbrevs = Abbreviations::empty();
138138

139139
loop {
140-
let (rest, abbrev) = try!(Abbreviation::parse(input));
140+
let (rest, abbrev) = Abbreviation::parse(input)?;
141141
input = rest;
142142

143143
match abbrev {
@@ -210,7 +210,7 @@ impl Abbreviation {
210210

211211
/// Parse an abbreviation's tag.
212212
fn parse_tag(input: &[u8]) -> Result<(&[u8], constants::DwTag)> {
213-
let (rest, val) = try!(parse_unsigned_leb(input));
213+
let (rest, val) = parse_unsigned_leb(input)?;
214214
if val == 0 {
215215
Err(Error::AbbreviationTagZero)
216216
} else {
@@ -220,7 +220,7 @@ impl Abbreviation {
220220

221221
/// Parse an abbreviation's "does the type have children?" byte.
222222
fn parse_has_children(input: &[u8]) -> Result<(&[u8], constants::DwChildren)> {
223-
let (rest, val) = try!(parse_u8(input));
223+
let (rest, val) = parse_u8(input)?;
224224
let val = constants::DwChildren(val);
225225
if val == constants::DW_CHILDREN_no || val == constants::DW_CHILDREN_yes {
226226
Ok((rest, val))
@@ -235,7 +235,7 @@ impl Abbreviation {
235235
let mut attrs = Vec::new();
236236

237237
loop {
238-
let (rest, attr) = try!(AttributeSpecification::parse(input));
238+
let (rest, attr) = AttributeSpecification::parse(input)?;
239239
input = rest;
240240

241241
match attr {
@@ -250,14 +250,14 @@ impl Abbreviation {
250250
/// Parse an abbreviation. Return `None` for the null abbreviation, `Some`
251251
/// for an actual abbreviation.
252252
fn parse(input: &[u8]) -> Result<(&[u8], Option<Abbreviation>)> {
253-
let (rest, code) = try!(parse_unsigned_leb(input));
253+
let (rest, code) = parse_unsigned_leb(input)?;
254254
if code == 0 {
255255
return Ok((rest, None));
256256
}
257257

258-
let (rest, tag) = try!(Self::parse_tag(rest));
259-
let (rest, has_children) = try!(Self::parse_has_children(rest));
260-
let (rest, attributes) = try!(Self::parse_attributes(rest));
258+
let (rest, tag) = Self::parse_tag(rest)?;
259+
let (rest, has_children) = Self::parse_has_children(rest)?;
260+
let (rest, attributes) = Self::parse_attributes(rest)?;
261261
let abbrev = Abbreviation::new(code, tag, has_children, attributes);
262262
Ok((rest, Some(abbrev)))
263263
}
@@ -345,7 +345,7 @@ impl AttributeSpecification {
345345

346346
/// Parse an attribute's form.
347347
fn parse_form(input: &[u8]) -> Result<(&[u8], constants::DwForm)> {
348-
let (rest, val) = try!(parse_unsigned_leb(input));
348+
let (rest, val) = parse_unsigned_leb(input)?;
349349
if val == 0 {
350350
Err(Error::AttributeFormZero)
351351
} else {
@@ -356,10 +356,10 @@ impl AttributeSpecification {
356356
/// Parse an attribute specification. Returns `None` for the null attribute
357357
/// specification, `Some` for an actual attribute specification.
358358
fn parse(input: &[u8]) -> Result<(&[u8], Option<AttributeSpecification>)> {
359-
let (rest, name) = try!(parse_unsigned_leb(input));
359+
let (rest, name) = parse_unsigned_leb(input)?;
360360
if name == 0 {
361361
// Parse the null attribute specification.
362-
let (rest, form) = try!(parse_unsigned_leb(rest));
362+
let (rest, form) = parse_unsigned_leb(rest)?;
363363
return if form == 0 {
364364
Ok((rest, None))
365365
} else {
@@ -368,7 +368,7 @@ impl AttributeSpecification {
368368
}
369369

370370
let name = constants::DwAt(name);
371-
let (rest, form) = try!(Self::parse_form(rest));
371+
let (rest, form) = Self::parse_form(rest)?;
372372
let spec = AttributeSpecification::new(name, form);
373373
Ok((rest, Some(spec)))
374374
}
@@ -440,11 +440,10 @@ pub mod tests {
440440
AttributeSpecification::new(constants::DW_AT_language,
441441
constants::DW_FORM_data2)]);
442442

443-
let abbrev2 =
444-
Abbreviation::new(2,
445-
constants::DW_TAG_subprogram,
446-
constants::DW_CHILDREN_no,
447-
vec![AttributeSpecification::new(constants::DW_AT_name,
443+
let abbrev2 = Abbreviation::new(2,
444+
constants::DW_TAG_subprogram,
445+
constants::DW_CHILDREN_no,
446+
vec![AttributeSpecification::new(constants::DW_AT_name,
448447
constants::DW_FORM_string)]);
449448

450449
let debug_abbrev = DebugAbbrev::<LittleEndian>::new(&buf);
@@ -574,11 +573,10 @@ pub mod tests {
574573
AttributeSpecification::new(constants::DW_AT_language,
575574
constants::DW_FORM_data2)]);
576575

577-
let abbrev2 =
578-
Abbreviation::new(2,
579-
constants::DW_TAG_subprogram,
580-
constants::DW_CHILDREN_no,
581-
vec![AttributeSpecification::new(constants::DW_AT_name,
576+
let abbrev2 = Abbreviation::new(2,
577+
constants::DW_TAG_subprogram,
578+
constants::DW_CHILDREN_no,
579+
vec![AttributeSpecification::new(constants::DW_AT_name,
582580
constants::DW_FORM_string)]);
583581

584582
let (rest, abbrevs) = Abbreviations::parse(&buf).expect("Should parse abbreviations");
@@ -651,11 +649,10 @@ pub mod tests {
651649
.get_contents()
652650
.unwrap();
653651

654-
let expect =
655-
Some(Abbreviation::new(1,
656-
constants::DW_TAG_subprogram,
657-
constants::DW_CHILDREN_no,
658-
vec![AttributeSpecification::new(constants::DW_AT_name,
652+
let expect = Some(Abbreviation::new(1,
653+
constants::DW_TAG_subprogram,
654+
constants::DW_CHILDREN_no,
655+
vec![AttributeSpecification::new(constants::DW_AT_name,
659656
constants::DW_FORM_string)]));
660657

661658
let (rest, abbrev) = Abbreviation::parse(&buf).expect("Should parse abbreviation");

src/aranges.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,21 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia
110110
#[allow(type_complexity)]
111111
fn parse_header(input: EndianBuf<Endian>)
112112
-> Result<(EndianBuf<Endian>, EndianBuf<Endian>, Rc<Self::Header>)> {
113-
let (rest, (length, format)) = try!(parse_initial_length(input));
113+
let (rest, (length, format)) = parse_initial_length(input)?;
114114
if length as usize > rest.len() {
115115
return Err(Error::UnexpectedEof);
116116
}
117117
let after_set = rest.range_from(length as usize..);
118118
let rest = rest.range_to(..length as usize);
119119

120-
let (rest, version) = try!(parse_u16(rest));
120+
let (rest, version) = parse_u16(rest)?;
121121
if version != 2 {
122122
return Err(Error::UnknownVersion);
123123
}
124124

125-
let (rest, offset) = try!(parse_debug_info_offset(rest, format));
126-
let (rest, address_size) = try!(parse_address_size(rest));
127-
let (rest, segment_size) = try!(parse_address_size(rest));
125+
let (rest, offset) = parse_debug_info_offset(rest, format)?;
126+
let (rest, address_size) = parse_address_size(rest)?;
127+
let (rest, segment_size) = parse_address_size(rest)?;
128128

129129
// unit_length + version + offset + address_size + segment_size
130130
let header_length = match format {
@@ -171,12 +171,12 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia
171171
}
172172

173173
let (rest, segment) = if segment_size != 0 {
174-
try!(parse_address(input, segment_size))
174+
parse_address(input, segment_size)?
175175
} else {
176176
(input, 0)
177177
};
178-
let (rest, address) = try!(parse_address(rest, address_size));
179-
let (rest, length) = try!(parse_address(rest, address_size));
178+
let (rest, address) = parse_address(rest, address_size)?;
179+
let (rest, length) = parse_address(rest, address_size)?;
180180

181181
match (segment, address, length) {
182182
// There may be multiple sets of tuples, each terminated by a zero tuple.

0 commit comments

Comments
 (0)