gimli-rs / gimli

A library for reading and writing the DWARF debugging format
https://docs.rs/gimli/
Apache License 2.0
831 stars 105 forks source link

Add lifetime to Section references #699

Closed crzysdrs closed 6 months ago

crzysdrs commented 6 months ago

By providing the lifetime of the Sections struct to the closure, the user can hold references to the data if neccesary.

Motivating Example:

use gimli::write::{DwarfUnit, EndianVec, Sections};

fn main() {
    let encoding = gimli::Encoding {
        format: gimli::Format::Dwarf32,
        version: 5,
        address_size: 8,
    };
    // Create a container for a single compilation unit.
    let mut dwarf = DwarfUnit::new(encoding);

    let mut sections = Sections::new(EndianVec::new(gimli::LittleEndian));
    // Finally, write the DWARF data to the sections.
    dwarf.write(&mut sections).unwrap();
    {
        let mut vec: Vec<&[u8]> = vec![];
        sections
            .for_each(|_id, data| {
                vec.push(data.slice());
                Result::<(), ()>::Ok(())
            })
            .unwrap();
        println!("{}", vec.len());
    }
    {
        let mut vec: Vec<&mut _> = vec![];
        sections
            .for_each_mut(|_id, data| {
                vec.push(data);
                Result::<(), ()>::Ok(())
            })
            .unwrap();
        println!("{}", vec.len());
    }
}