Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ impl CppStruct {

let default = if writer.config.sys {
quote! {}
} else if self.can_derive_default() {
derive.extend(["Default"]);
quote! {}
} else {
quote! {
#cfg
Expand Down Expand Up @@ -224,6 +227,16 @@ impl CppStruct {
tokens
}

fn can_derive_default(&self) -> bool {
!self.has_explicit_layout()
&& self.def.fields().all(|field| {
!matches!(
field.ty(Some(self)),
Type::ArrayFixed(..) | Type::PtrConst(..) | Type::PtrMut(..)
)
})
Comment on lines +232 to +237
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. 🙃

}

pub fn is_copyable(&self) -> bool {
if matches!(
self.def.type_name(),
Expand Down
6 changes: 6 additions & 0 deletions crates/libs/strings/src/pcstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ impl PCSTR {
unsafe { Decode(move || decode_utf8(self.as_bytes())) }
}
}

impl Default for PCSTR {
fn default() -> Self {
Self::null()
}
}
6 changes: 6 additions & 0 deletions crates/libs/strings/src/pcwstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl PCWSTR {
}
}

impl Default for PCWSTR {
fn default() -> Self {
Self::null()
}
}

impl AsRef<PCWSTR> for PCWSTR {
fn as_ref(&self) -> &Self {
self
Expand Down
6 changes: 6 additions & 0 deletions crates/libs/strings/src/pstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ impl PSTR {
unsafe { Decode(move || decode_utf8(self.as_bytes())) }
}
}

impl Default for PSTR {
fn default() -> Self {
Self::null()
}
}
6 changes: 6 additions & 0 deletions crates/libs/strings/src/pwstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ impl PWSTR {
unsafe { Decode(move || core::char::decode_utf16(self.as_wide().iter().cloned())) }
}
}

impl Default for PWSTR {
fn default() -> Self {
Self::null()
}
}
Loading