Skip to content

Commit

Permalink
Use the try operator
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc committed Jun 13, 2017
1 parent bdac366 commit 2151637
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 380 deletions.
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
write_mode = "Overwrite"

use_try_shorthand = true
51 changes: 24 additions & 27 deletions src/abbrev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Abbreviations {
let mut abbrevs = Abbreviations::empty();

loop {
let (rest, abbrev) = try!(Abbreviation::parse(input));
let (rest, abbrev) = Abbreviation::parse(input)?;
input = rest;

match abbrev {
Expand Down Expand Up @@ -210,7 +210,7 @@ impl Abbreviation {

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

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

loop {
let (rest, attr) = try!(AttributeSpecification::parse(input));
let (rest, attr) = AttributeSpecification::parse(input)?;
input = rest;

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

let (rest, tag) = try!(Self::parse_tag(rest));
let (rest, has_children) = try!(Self::parse_has_children(rest));
let (rest, attributes) = try!(Self::parse_attributes(rest));
let (rest, tag) = Self::parse_tag(rest)?;
let (rest, has_children) = Self::parse_has_children(rest)?;
let (rest, attributes) = Self::parse_attributes(rest)?;
let abbrev = Abbreviation::new(code, tag, has_children, attributes);
Ok((rest, Some(abbrev)))
}
Expand Down Expand Up @@ -345,7 +345,7 @@ impl AttributeSpecification {

/// Parse an attribute's form.
fn parse_form(input: &[u8]) -> Result<(&[u8], constants::DwForm)> {
let (rest, val) = try!(parse_unsigned_leb(input));
let (rest, val) = parse_unsigned_leb(input)?;
if val == 0 {
Err(Error::AttributeFormZero)
} else {
Expand All @@ -356,10 +356,10 @@ impl AttributeSpecification {
/// Parse an attribute specification. Returns `None` for the null attribute
/// specification, `Some` for an actual attribute specification.
fn parse(input: &[u8]) -> Result<(&[u8], Option<AttributeSpecification>)> {
let (rest, name) = try!(parse_unsigned_leb(input));
let (rest, name) = parse_unsigned_leb(input)?;
if name == 0 {
// Parse the null attribute specification.
let (rest, form) = try!(parse_unsigned_leb(rest));
let (rest, form) = parse_unsigned_leb(rest)?;
return if form == 0 {
Ok((rest, None))
} else {
Expand All @@ -368,7 +368,7 @@ impl AttributeSpecification {
}

let name = constants::DwAt(name);
let (rest, form) = try!(Self::parse_form(rest));
let (rest, form) = Self::parse_form(rest)?;
let spec = AttributeSpecification::new(name, form);
Ok((rest, Some(spec)))
}
Expand Down Expand Up @@ -440,11 +440,10 @@ pub mod tests {
AttributeSpecification::new(constants::DW_AT_language,
constants::DW_FORM_data2)]);

let abbrev2 =
Abbreviation::new(2,
constants::DW_TAG_subprogram,
constants::DW_CHILDREN_no,
vec![AttributeSpecification::new(constants::DW_AT_name,
let abbrev2 = Abbreviation::new(2,
constants::DW_TAG_subprogram,
constants::DW_CHILDREN_no,
vec![AttributeSpecification::new(constants::DW_AT_name,
constants::DW_FORM_string)]);

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

let abbrev2 =
Abbreviation::new(2,
constants::DW_TAG_subprogram,
constants::DW_CHILDREN_no,
vec![AttributeSpecification::new(constants::DW_AT_name,
let abbrev2 = Abbreviation::new(2,
constants::DW_TAG_subprogram,
constants::DW_CHILDREN_no,
vec![AttributeSpecification::new(constants::DW_AT_name,
constants::DW_FORM_string)]);

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

let expect =
Some(Abbreviation::new(1,
constants::DW_TAG_subprogram,
constants::DW_CHILDREN_no,
vec![AttributeSpecification::new(constants::DW_AT_name,
let expect = Some(Abbreviation::new(1,
constants::DW_TAG_subprogram,
constants::DW_CHILDREN_no,
vec![AttributeSpecification::new(constants::DW_AT_name,
constants::DW_FORM_string)]));

let (rest, abbrev) = Abbreviation::parse(&buf).expect("Should parse abbreviation");
Expand Down
16 changes: 8 additions & 8 deletions src/aranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,21 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia
#[allow(type_complexity)]
fn parse_header(input: EndianBuf<Endian>)
-> Result<(EndianBuf<Endian>, EndianBuf<Endian>, Rc<Self::Header>)> {
let (rest, (length, format)) = try!(parse_initial_length(input));
let (rest, (length, format)) = parse_initial_length(input)?;
if length as usize > rest.len() {
return Err(Error::UnexpectedEof);
}
let after_set = rest.range_from(length as usize..);
let rest = rest.range_to(..length as usize);

let (rest, version) = try!(parse_u16(rest));
let (rest, version) = parse_u16(rest)?;
if version != 2 {
return Err(Error::UnknownVersion);
}

let (rest, offset) = try!(parse_debug_info_offset(rest, format));
let (rest, address_size) = try!(parse_address_size(rest));
let (rest, segment_size) = try!(parse_address_size(rest));
let (rest, offset) = parse_debug_info_offset(rest, format)?;
let (rest, address_size) = parse_address_size(rest)?;
let (rest, segment_size) = parse_address_size(rest)?;

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

let (rest, segment) = if segment_size != 0 {
try!(parse_address(input, segment_size))
parse_address(input, segment_size)?
} else {
(input, 0)
};
let (rest, address) = try!(parse_address(rest, address_size));
let (rest, length) = try!(parse_address(rest, address_size));
let (rest, address) = parse_address(rest, address_size)?;
let (rest, length) = parse_address(rest, address_size)?;

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

0 comments on commit 2151637

Please sign in to comment.