Skip to content

Replace UnalignedCStr16 with UnalignedSlice #539

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

Merged
merged 3 commits into from
Oct 30, 2022

Conversation

nicholasbishop
Copy link
Member

@nicholasbishop nicholasbishop commented Oct 29, 2022

I added UnalignedCStr16 a while ago for use with device path nodes, which sometimes end in a DST string. Since device path nodes are all packed structures, the DST slice may be unaligned, so you can't return a normal reference as that would be UB.

Some device path nodes end with DST slices that aren't strings. For example, ACPI _ADR ends with a [u32] slice. So we need something more general than UnalignedCStr16, which is what UnalignedSlice is. An UnalignedSlice<u16> provides the same to_cstr16 and to_cstring16 methods as UnalignedCStr16, so for existing caller's this should be a pretty transparent change.

Checklist

  • Sensible git history (for example, squash "typo" or "fix" commits). See the Rewriting History guide for help.
  • Update the changelog (if necessary)

This is a generalized version of `UnalignedCStr16`. It's similar to
`&[T]`, except it can point to an unaligned slice. This is useful with
device path nodes which sometimes end with a DST slice. Since device
path nodes are all packed structures, the DST slice may be unaligned.

`UnalignedSlice` tries to generally follow the API of a regular slice,
with some deviations where necessary (for example it returns copies
rather than references, and provides a `copy_to` method instead of
`copy_from_slice`).
This should mostly be transparent to callers. An `UnalignedSlice<u16>`
slice provides the same `to_cstr16` and `to_cstring16` methods that
`UnalignedCStr16` did.
@phip1611
Copy link
Member

LGTM, thanks :)

@phip1611 phip1611 merged commit 93608b2 into rust-osdev:main Oct 30, 2022
@nicholasbishop nicholasbishop deleted the bishop-unaligned-slice-1 branch October 31, 2022 01:56
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