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 Default for more structs #3519

Merged
merged 2 commits into from
Feb 26, 2025
Merged

Derive Default for more structs #3519

merged 2 commits into from
Feb 26, 2025

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Feb 26, 2025

There are cases where Default cannot be derived for certain Windows types, but there are many more where it can and doing so improves readability and reduces code generation. This further builds on the likes of #2760, #3015, #3041, #3097, #3270 by deriving Default for structs that don't use the --sys option, although I have had a request to extend it to --sys code generation as well so I'll probably do that in a follow-up PR.

Notably, Rust won't allow Default to be derived for structs with explicit layout or structs with pointer or array fields. These continue to be generated by windows-bindgen as before.

@kennykerr kennykerr changed the title Derive Default where possible Derive Default for more structs Feb 26, 2025
@kennykerr kennykerr requested a review from dpaoliello February 26, 2025 17:25
Comment on lines +232 to +237
&& self.def.fields().all(|field| {
!matches!(
field.ty(Some(self)),
Type::ArrayFixed(..) | Type::PtrConst(..) | Type::PtrMut(..)
)
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if this is easier to read?

Suggested change
&& self.def.fields().all(|field| {
!matches!(
field.ty(Some(self)),
Type::ArrayFixed(..) | Type::PtrConst(..) | Type::PtrMut(..)
)
})
&& !self.def.fields().any(|field| {
matches!(
field.ty(Some(self)),
Type::ArrayFixed(..) | Type::PtrConst(..) | Type::PtrMut(..)
)
})

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure either. 🙃

@kennykerr kennykerr merged commit fbc8fb1 into master Feb 26, 2025
34 checks passed
@kennykerr kennykerr deleted the derive-default branch February 26, 2025 18:44
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.

2 participants