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 PartialEq, Eq, Debug, Clone for interfaces #2651

Merged
merged 3 commits into from
Sep 12, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn gen_class(writer: &Writer, def: TypeDef) -> TokenStream {
#doc
#features
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
pub struct #name(::windows_core::IUnknown);
#features
impl #name {
Expand All @@ -95,7 +96,6 @@ fn gen_class(writer: &Writer, def: TypeDef) -> TokenStream {
}
};

tokens.combine(&writer.interface_core_traits(def, &[], &name, &TokenStream::new(), &TokenStream::new(), &features));
tokens.combine(&writer.interface_winrt_trait(def, &[], &name, &TokenStream::new(), &TokenStream::new(), &features));
tokens.combine(&writer.interface_trait(def, &[], &name, &TokenStream::new(), &features, true));
tokens.combine(&writer.runtime_name_trait(def, &[], &name, &TokenStream::new(), &features));
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/delegates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn gen_win_delegate(writer: &Writer, def: TypeDef) -> TokenStream {
#doc
#features
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
pub struct #ident(pub ::windows_core::IUnknown, #phantoms) where #constraints;
#features
impl<#constraints> #ident {
Expand Down Expand Up @@ -142,7 +143,6 @@ fn gen_win_delegate(writer: &Writer, def: TypeDef) -> TokenStream {
}
};

tokens.combine(&writer.interface_core_traits(def, generics, &ident, &constraints, &phantoms, &features));
tokens.combine(&writer.interface_trait(def, generics, &ident, &constraints, &features, true));
tokens.combine(&writer.interface_winrt_trait(def, generics, &ident, &constraints, &phantoms, &features));
tokens.combine(&writer.interface_vtbl(def, generics, &ident, &constraints, &features));
Expand Down
3 changes: 2 additions & 1 deletion crates/libs/bindgen/src/rust/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ fn gen_win_interface(writer: &Writer, def: TypeDef) -> TokenStream {
tokens.combine(&quote! {
#features
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
pub struct #ident(::windows_core::IUnknown, #phantoms) where #constraints;
});
} else {
tokens.combine(&quote! {
#features
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
pub struct #ident(::std::ptr::NonNull<::std::ffi::c_void>);
});
}
Expand Down Expand Up @@ -136,7 +138,6 @@ fn gen_win_interface(writer: &Writer, def: TypeDef) -> TokenStream {
}
}

tokens.combine(&writer.interface_core_traits(def, generics, &ident, &constraints, &phantoms, &features));
tokens.combine(&writer.interface_winrt_trait(def, generics, &ident, &constraints, &phantoms, &features));
tokens.combine(&writer.async_get(def, generics, &ident, &constraints, &phantoms, &features));
tokens.combine(&iterators::writer(writer, def, generics, &ident, &constraints, &phantoms, &cfg));
Expand Down
33 changes: 0 additions & 33 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,25 +537,6 @@ impl<'a> Writer<'a> {
let guid = self.type_name(&Type::GUID);
format!("{}::from_u128(0x{:08x?}_{:04x?}_{:04x?}_{:02x?}{:02x?}_{:02x?}{:02x?}{:02x?}{:02x?}{:02x?}{:02x?})", guid.into_string(), value.0, value.1, value.2, value.3, value.4, value.5, value.6, value.7, value.8, value.9, value.10).into()
}
pub fn interface_core_traits(&self, def: TypeDef, _generics: &[Type], ident: &TokenStream, constraints: &TokenStream, _phantoms: &TokenStream, features: &TokenStream) -> TokenStream {
let name = crate::trim_tick(self.reader.type_def_name(def));
quote! {
#features
impl<#constraints> ::core::cmp::PartialEq for #ident {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
#features
impl<#constraints> ::core::cmp::Eq for #ident {}
#features
impl<#constraints> ::core::fmt::Debug for #ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple(#name).field(&self.0).finish()
}
}
}
}
pub fn agile(&self, def: TypeDef, ident: &TokenStream, constraints: &TokenStream, features: &TokenStream) -> TokenStream {
if type_def_is_agile(self.reader, def) {
quote! {
Expand Down Expand Up @@ -718,12 +699,6 @@ impl<'a> Writer<'a> {
let default_name = self.type_name(&default);
let vtbl = self.type_vtbl_name(&default);
quote! {
#features
impl<#constraints> ::core::clone::Clone for #ident {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
#features
unsafe impl ::windows_core::Interface for #ident {
type Vtable = #vtbl;
Expand All @@ -750,19 +725,11 @@ impl<'a> Writer<'a> {
}
};

let phantoms = self.generic_phantoms(generics);

let mut tokens = quote! {
#features
unsafe impl<#constraints> ::windows_core::Interface for #ident {
type Vtable = #vtbl;
}
#features
impl<#constraints> ::core::clone::Clone for #ident {
fn clone(&self) -> Self {
Self(self.0.clone(), #phantoms)
}
}
};

if has_unknown_base {
Expand Down
Loading