diff --git a/crates/examples/Cargo.toml b/crates/examples/Cargo.toml index 8406d797..fa74cbd8 100644 --- a/crates/examples/Cargo.toml +++ b/crates/examples/Cargo.toml @@ -24,11 +24,11 @@ default = ["read", "std", "fallible-iterator"] [[bin]] name = "simple" -required-features = ["read"] +required-features = ["read", "std"] [[bin]] name = "simple_line" -required-features = ["read"] +required-features = ["read", "std"] [[bin]] name = "dwarfdump" diff --git a/crates/examples/src/bin/simple.rs b/crates/examples/src/bin/simple.rs index 623e43ae..046747d3 100644 --- a/crates/examples/src/bin/simple.rs +++ b/crates/examples/src/bin/simple.rs @@ -8,7 +8,7 @@ //! file and DWP file, which is not something that is provided by gimli itself. use object::{Object, ObjectSection}; -use std::{borrow, env, fs}; +use std::{borrow, env, error, fs}; fn main() { let mut args = env::args(); @@ -45,44 +45,38 @@ fn dump_file( object: &object::File, dwp_object: Option<&object::File>, endian: gimli::RunTimeEndian, -) -> Result<(), gimli::Error> { +) -> Result<(), Box> { // Load a section and return as `Cow<[u8]>`. fn load_section<'a>( object: &'a object::File, name: &str, - ) -> Result, gimli::Error> { - match object.section_by_name(name) { - Some(ref section) => Ok(section - .uncompressed_data() - .unwrap_or(borrow::Cow::Borrowed(&[][..]))), - None => Ok(borrow::Cow::Borrowed(&[][..])), - } + ) -> Result, Box> { + Ok(match object.section_by_name(name) { + Some(section) => section.uncompressed_data()?, + None => borrow::Cow::Borrowed(&[]), + }) } // Borrow a `Cow<[u8]>` to create an `EndianSlice`. - let borrow_section: &dyn for<'a> Fn( - &'a borrow::Cow<[u8]>, - ) -> gimli::EndianSlice<'a, gimli::RunTimeEndian> = - &|section| gimli::EndianSlice::new(section, endian); + let borrow_section = |section| gimli::EndianSlice::new(borrow::Cow::as_ref(section), endian); // Load all of the sections. let dwarf_sections = gimli::DwarfSections::load(|id| load_section(object, id.name()))?; - let dwp_sections = if let Some(dwp_object) = dwp_object { - Some(gimli::DwarfPackageSections::load(|id| { - load_section(dwp_object, id.dwo_name().unwrap()) - })?) - } else { - None - }; + let dwp_sections = dwp_object + .map(|dwp_object| { + gimli::DwarfPackageSections::load(|id| load_section(dwp_object, id.dwo_name().unwrap())) + }) + .transpose()?; - // Create `EndianSlice`s for all of the sections. + // Create `EndianSlice`s for all of the sections and do preliminary parsing. // Alternatively, we could have used `Dwarf::load` with an owned type such as `EndianRcSlice`. - let dwarf = dwarf_sections.borrow(&borrow_section); - let dwp = if let Some(dwp_sections) = &dwp_sections { - Some(dwp_sections.borrow(&borrow_section, gimli::EndianSlice::new(&[][..], endian))?) - } else { - None - }; + let dwarf = dwarf_sections.borrow(borrow_section); + let dwp = dwp_sections + .as_ref() + .map(|dwp_sections| { + dwp_sections.borrow(borrow_section, gimli::EndianSlice::new(&[], endian)) + }) + .transpose()?; // Iterate over the compilation units. let mut iter = dwarf.units(); diff --git a/crates/examples/src/bin/simple_line.rs b/crates/examples/src/bin/simple_line.rs index ec4ca964..8b0165ed 100644 --- a/crates/examples/src/bin/simple_line.rs +++ b/crates/examples/src/bin/simple_line.rs @@ -1,7 +1,7 @@ //! A simple example of parsing `.debug_line`. use object::{Object, ObjectSection}; -use std::{borrow, env, fs, path}; +use std::{borrow, env, error, fs, path}; fn main() { for path in env::args().skip(1) { @@ -17,28 +17,26 @@ fn main() { } } -fn dump_file(object: &object::File, endian: gimli::RunTimeEndian) -> Result<(), gimli::Error> { +fn dump_file( + object: &object::File, + endian: gimli::RunTimeEndian, +) -> Result<(), Box> { // Load a section and return as `Cow<[u8]>`. - let load_section = |id: gimli::SectionId| -> Result, gimli::Error> { - match object.section_by_name(id.name()) { - Some(ref section) => Ok(section - .uncompressed_data() - .unwrap_or(borrow::Cow::Borrowed(&[][..]))), - None => Ok(borrow::Cow::Borrowed(&[][..])), - } + let load_section = |id: gimli::SectionId| -> Result, Box> { + Ok(match object.section_by_name(id.name()) { + Some(section) => section.uncompressed_data()?, + None => borrow::Cow::Borrowed(&[]), + }) }; - // Load all of the sections. - let dwarf_cow = gimli::DwarfSections::load(&load_section)?; - // Borrow a `Cow<[u8]>` to create an `EndianSlice`. - let borrow_section: &dyn for<'a> Fn( - &'a borrow::Cow<[u8]>, - ) -> gimli::EndianSlice<'a, gimli::RunTimeEndian> = - &|section| gimli::EndianSlice::new(section, endian); + let borrow_section = |section| gimli::EndianSlice::new(borrow::Cow::as_ref(section), endian); + + // Load all of the sections. + let dwarf_sections = gimli::DwarfSections::load(&load_section)?; // Create `EndianSlice`s for all of the sections. - let dwarf = dwarf_cow.borrow(&borrow_section); + let dwarf = dwarf_sections.borrow(borrow_section); // Iterate over the compilation units. let mut iter = dwarf.units();