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] Support IntoBytes on some generic structs #862

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Feb 10, 2024

In particular, support IntoBytes on structs with generic type parameters so long as:

  • The type is repr(C) and has zero or one fields
  • All field types (including concrete types) implement Unaligned

Closes #825

@joshlf joshlf force-pushed the derive-as-bytes-generic-unaligned branch 4 times, most recently from 196982e to ee14390 Compare February 10, 2024 21:07
@joshlf joshlf marked this pull request as ready for review February 10, 2024 21:08
@joshlf joshlf requested a review from jswrenn February 10, 2024 21:08
joshlf added a commit that referenced this pull request Feb 10, 2024
In particular, support `AsBytes` on structs with generic type
parameters so long as:
- The type is `repr(C)` and has zero or one fields
- All field types (including concrete types) implement `Unaligned`

This change was backported from #862
&ast,
strct,
Trait::NoCell,
FieldBounds::All(&[Trait::NoCell]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a Trait::Self? The correctness of all of these derives depends on the FieldBounds::All(&[Trait::XXX]) having the same Trait::XXX as the parameter before it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Trait::Self would require adding panic paths in impl_block (namely, it would be invalid to pass Trait::Self as the trt argument - the trait to be implemented), so I added a separate TraitBound enum that should have the same effect without introducing panic paths. Lmk if that looks better.

@joshlf joshlf force-pushed the derive-as-bytes-generic-unaligned branch 2 times, most recently from fae2cab to 3897534 Compare February 12, 2024 17:56
In particular, support `IntoBytes` on structs with generic type
parameters so long as:
- The type is `repr(C)` and has zero or one fields
- All field types (including concrete types) implement `Unaligned`

Closes #825
@joshlf
Copy link
Member Author

joshlf commented Feb 12, 2024

Filed this to track improving the error messages: #868

@joshlf joshlf added this pull request to the merge queue Feb 12, 2024
Merged via the queue into main with commit 65f2e3e Feb 12, 2024
209 checks passed
@joshlf joshlf deleted the derive-as-bytes-generic-unaligned branch February 12, 2024 19:35
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.

Optionally derive IntoBytes for repr(C) generic types using Unaligned constraints on fields
2 participants