[0.2] Backport "cleanup: Use derived traits as much as possible"#4814
Merged
tgross35 merged 1 commit intorust-lang:libc-0.2from Nov 4, 2025
Merged
Conversation
e2e2b7f to
ef86fc1
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. (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 ]
ef86fc1 to
1f9b825
Compare
Collaborator
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport for #4806