-
Notifications
You must be signed in to change notification settings - Fork 1.2k
cleanup: Use derived traits as much as possible #4806
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4a6d93f to
f28ac38
Compare
7a72d95 to
3025a53
Compare
3025a53 to
f9329a5
Compare
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future.
f9329a5 to
3ce0190
Compare
3 tasks
|
@rustbot label +stable-nominated |
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future. (backport <rust-lang#4806>) (cherry picked from commit 141aea1) [ note that this was very conflict-heavy so the cherry pick differs quite a bit from the original commit - Trevor ]
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future. (backport <rust-lang#4806>) (cherry picked from commit 141aea1) [ note that this was very conflict-heavy so the cherry pick differs quite a bit from the original commit - Trevor ]
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future. (backport <rust-lang#4806>) (cherry picked from commit 141aea1) [ note that this was very conflict-heavy so the cherry pick differs quite a bit from the original commit - Trevor ]
tgross35
added a commit
to tgross35/rust-libc
that referenced
this pull request
Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future. (backport <rust-lang#4806>) (cherry picked from commit 141aea1) [ note that this was very conflict-heavy so the cherry pick differs quite a bit from the original commit - Trevor ]
github-merge-queue bot
pushed a commit
that referenced
this pull request
Nov 4, 2025
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future. (backport <#4806>) (cherry picked from commit 141aea1) [ note that this was very conflict-heavy so the cherry pick differs quite a bit from the original commit - Trevor ]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
O-android
O-arm
O-bsd
O-dragonfly
O-gnu
O-illumos
O-linux
O-linux-like
O-macos
O-musl
O-newlib
O-powerpc
O-redox
O-riscv
O-solarish
O-unix
O-x86
stable-nominated
This PR should be considered for cherry-pick to libc's stable release branch
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Historically Rust hasn't supported derives on packed structs well, and
until recently our automatic derives via
s!still did not supportthese (fixed by 79d1bdb ("Flip
CopyandPartialEq, Hashderives")). This meant that any packed struct implementing
extra_traitshad handwritten implementations that are no longerneeded.
There are also a handful of cases that use a handwritten implementation
in order to selectively exclude padding fields. Others don't seem to be
handwritten for any specific reason.
Clean up all of the above by switching from
s_no_extra_traits!tos!and deleting the handwritten implementations wherever possible. The only
remaining handwritten implementations are those that compare unions with
unsafe(which will have to be addressed) or floats.Note that in some cases, this means we are no longer excluding padding
fields from comparisons. These traits are only usable via references,
not raw pointers; thus, the user has asserted that they are initialized,
and there is no UB in
libcby reading them. Practically the only wayto know they are initialized is by starting with a zeroed struct, so in
most cases they will have no effect on the results. Whether or not
padding fields are included in handwritten traits is also completely
inconsistent (most include them), so there isn't much predictability
lost here.
The new
Paddingtype will also improve the situation here in thefuture.