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

[derive] Derive TryFromBytes on unions #800

Merged
merged 1 commit into from
Jan 21, 2024
Merged

Conversation

jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Jan 21, 2024

The implementation itself is very straightforward, but it requires a few prerequisite changes:

  • The Ptr::project method now requires that it only be called on struct and union types
  • The AsInitialized invariant property is changed in a subtle but important way. Previously, a byte offset in a type was required to be initialized if, in every instance of that type, it was initialized. Now, this is required if the byte is initialized in /any/ instance of the type. Without this change, it would be possible to invoke TryFromBytes::is_bit_valid on a byte sequence where an uninitialized byte appears which is valid for one union field but not valid for another. Since we check bit validity for all union fields, uninitialized bytes may only appear which are compatible with /all/ union fields.

@joshlf joshlf self-requested a review January 21, 2024 00:45
@joshlf joshlf enabled auto-merge January 21, 2024 00:45
joshlf
joshlf previously approved these changes Jan 21, 2024
The implementation itself is very straightforward, but it requires a few
prerequisite changes:
- The `Ptr::project` method now requires that it only be called on
  struct and union types
- The `AsInitialized` invariant property is changed in a subtle but
  important way. Previously, a byte offset in a type was required to be
  initialized if, in every instance of that type, it was initialized.
  Now, this is required if the byte is initialized in /any/ instance of
  the type. Without this change, it would be possible to invoke
  `TryFromBytes::is_bit_valid` on a byte sequence where an uninitialized
  byte appears which is valid for one union field but not valid for
  another. Since we check bit validity for all union fields,
  uninitialized bytes may only appear which are compatible with /all/
  union fields.
@joshlf joshlf added this pull request to the merge queue Jan 21, 2024
Merged via the queue into main with commit c5ab376 Jan 21, 2024
127 checks passed
@joshlf joshlf deleted the try-from-bytes-unions branch January 21, 2024 01:12
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