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

feat: add ResourceName::raw_data method #487

Merged
merged 1 commit into from
Nov 20, 2022

Conversation

vthib
Copy link
Contributor

@vthib vthib commented Nov 19, 2022

In my program, I want to give access to the bytes of a resource name. Since this is a unicode string, there is already a method to get a slice of u16. I can use it to get back the raw bytes, but that requires unsafe code that can be avoided by getting the raw bytes.

So this PR adds a new method to ResourceName to get the name as a slice of bytes instead of a slice of u16.

@@ -166,6 +166,21 @@ impl ResourceName {
.read_slice::<U16Bytes<LE>>(&mut offset, len.get(LE).into())
.read_error("Invalid resource name length")
}

/// Returns the string buffer as raw bytes.
pub fn raw_data<'data>(&self, directory: ResourceDirectory<'data>) -> Result<&'data [u8]> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of duplicating logic, can we implement in terms of data. Something like self.data(directory).map(pod::bytes_of_slice).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes! I didn't know about bytes_of_slice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since bytes_of_slice is public, this oneliner can be done on the user (my) side, so this helper is not that very useful anymore. I'll let you decide if it makes sense to keep it or not

Gives access to the raw bytes of the name string, instead of a u16
slice.
@philipc philipc merged commit 9697729 into gimli-rs:master Nov 20, 2022
mcbegamerxx954 pushed a commit to mcbegamerxx954/object that referenced this pull request Jun 15, 2024
Gives access to the raw bytes of the name string, instead of a u16
slice.
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.

2 participants