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

Expose the inner value of Unalign as public #1860

Closed
kupiakos opened this issue Oct 10, 2024 · 2 comments
Closed

Expose the inner value of Unalign as public #1860

kupiakos opened this issue Oct 10, 2024 · 2 comments

Comments

@kupiakos
Copy link
Contributor

kupiakos commented Oct 10, 2024

This enables full field reference projection for fields that are sufficiently aligned and field read/write access for all fields and subfields. This must be done through direct struct field access for the compiler to accept it.

Sample use case

Say the input buffer you receive has bytes in the format of BigStruct, and our function reads specific fields out of those bytes based on a struct layout defined by bindgen. There are too many unread fields to reasonably copy it. However, the input buffer may be unaligned. So, you first access the prefix as a &Unalign<BigStruct>.

In order to access specific unaligned fields safely, you need one of the following:

  • Field projection
  • To copy the whole struct
  • To use unsafe
  • Have field access to the contents of the Unalign.

Currently, the workaround is to declare your own repr(packed) newtype that has an accessible inner value. But honestly, I should be able to use the built-in Unalign newtype for this. Rust will guard against an accidental reference to an unaligned field.

@kupiakos kupiakos changed the title Make the value in Unalign public Expose the inner value of Unalign as public Oct 10, 2024
@kupiakos
Copy link
Contributor Author

It might be worth gating the inner pub field based on Rust version since the lint producing a hard error for referencing an underaligned field was introduced in 1.69.

Additionally, if we add T: ?Sized to Unalign, then only in 1.76 was this lint fixed for unsized fields.

@joshlf
Copy link
Member

joshlf commented Nov 25, 2024

Merging this into #836 (comment)

@joshlf joshlf closed this as completed Nov 25, 2024
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

No branches or pull requests

2 participants