Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lifetime to Section references #699

Merged
merged 1 commit into from Mar 9, 2024
Merged

Conversation

crzysdrs
Copy link
Contributor

@crzysdrs crzysdrs commented Mar 8, 2024

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());
    }
}

Copy link
Collaborator

@philipc philipc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@philipc philipc merged commit 9ea890c into gimli-rs:master Mar 9, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants