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

Implement Debug trait for windows crate #1395

Merged
merged 15 commits into from Dec 23, 2021
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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -8,6 +8,7 @@ members = [
"crates/tests/winrt/*",
"crates/tests/win32/*",
"crates/tests/core",
"crates/tests/debug",
"crates/tests/sys",
]
exclude = ["crates/tests/component"]
31 changes: 19 additions & 12 deletions crates/libs/bindgen/src/enums.rs
@@ -1,7 +1,8 @@
use super::*;

pub fn gen(def: &TypeDef, gen: &Gen) -> TokenStream {
let name = gen_ident(def.name());
let name = def.name();
let ident = gen_ident(name);
let underlying_type = def.underlying_type();
let underlying_type = gen_element_name(&underlying_type, gen);
let is_scoped = def.is_scoped();
Expand Down Expand Up @@ -39,15 +40,15 @@ pub fn gen(def: &TypeDef, gen: &Gen) -> TokenStream {
#doc
#features
#[repr(transparent)]
pub struct #name(pub #underlying_type);
pub struct #ident(pub #underlying_type);
#features
impl #name {
impl #ident {
#(#fields)*
}
#features
impl ::core::marker::Copy for #name {}
impl ::core::marker::Copy for #ident {}
#features
impl ::core::clone::Clone for #name {
impl ::core::clone::Clone for #ident {
fn clone(&self) -> Self {
*self
}
Expand All @@ -58,14 +59,14 @@ pub fn gen(def: &TypeDef, gen: &Gen) -> TokenStream {
quote! {
#doc
#features
pub const #field_name: #name = #value;
pub const #field_name: #ident = #value;
}
});

quote! {
#doc
#features
pub type #name = #underlying_type;
pub type #ident = #underlying_type;
#(#fields)*
}
};
Expand All @@ -74,17 +75,23 @@ pub fn gen(def: &TypeDef, gen: &Gen) -> TokenStream {
if is_scoped {
tokens.combine(&quote! {
#features
unsafe impl ::windows::core::Abi for #name {
unsafe impl ::windows::core::Abi for #ident {
type Abi = Self;
}
#features
impl ::core::cmp::PartialEq for #name {
impl ::core::cmp::PartialEq for #ident {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
#features
impl ::core::cmp::Eq for #name {}
impl ::core::cmp::Eq for #ident {}
#features
impl ::core::fmt::Debug for #ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple(#name).field(&self.0).finish()
}
}
});
}

Expand All @@ -93,11 +100,11 @@ pub fn gen(def: &TypeDef, gen: &Gen) -> TokenStream {

tokens.combine(&quote! {
#features
unsafe impl ::windows::core::RuntimeType for #name {
unsafe impl ::windows::core::RuntimeType for #ident {
const SIGNATURE: ::windows::core::ConstBuffer = ::windows::core::ConstBuffer::from_slice(#signature);
}
#features
impl ::windows::core::DefaultType for #name {
impl ::windows::core::DefaultType for #ident {
type DefaultType = Self;
}
});
Expand Down
15 changes: 11 additions & 4 deletions crates/libs/bindgen/src/helpers.rs
@@ -1,26 +1,33 @@
use super::*;

pub fn gen_std_traits(def: &TypeDef, cfg: &Cfg, gen: &Gen) -> TokenStream {
let name = gen_type_ident(def, gen);
let ident = gen_type_ident(def, gen);
let name = ident.as_str();
let constraints = gen_type_constraints(def, gen);
let phantoms = gen_phantoms(def, gen);
let cfg = cfg.gen(gen);

quote! {
#cfg
impl<#(#constraints)*> ::core::clone::Clone for #name {
impl<#(#constraints)*> ::core::clone::Clone for #ident {
fn clone(&self) -> Self {
Self(self.0.clone(), #(#phantoms)*)
}
}
#cfg
impl<#(#constraints)*> ::core::cmp::PartialEq for #name {
impl<#(#constraints)*> ::core::cmp::PartialEq for #ident {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
#cfg
impl<#(#constraints)*> ::core::cmp::Eq for #name {}
impl<#(#constraints)*> ::core::cmp::Eq for #ident {}
#cfg
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()
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/libs/bindgen/src/replacements/bool32.rs
Expand Up @@ -55,6 +55,12 @@ pub fn gen() -> TokenStream {

impl ::core::cmp::Eq for BOOL {}

impl ::core::fmt::Debug for BOOL {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple("BOOL").field(&self.0).finish()
}
}

impl ::core::convert::From<BOOL> for bool {
fn from(value: BOOL) -> Self {
value.as_bool()
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/replacements/handle.rs
Expand Up @@ -34,6 +34,11 @@ pub fn gen() -> TokenStream {
}
}
impl ::core::cmp::Eq for HANDLE {}
impl ::core::fmt::Debug for HANDLE {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple("HANDLE").field(&self.0).finish()
}
}
unsafe impl ::windows::core::Abi for HANDLE {
type Abi = Self;
}
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/replacements/ntstatus.rs
Expand Up @@ -46,6 +46,11 @@ pub fn gen() -> TokenStream {
}
}
impl ::core::cmp::Eq for NTSTATUS {}
impl ::core::fmt::Debug for NTSTATUS {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple("NTSTATUS").field(&self.0).finish()
}
}
unsafe impl ::windows::core::Abi for NTSTATUS {
type Abi = Self;
}
Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/replacements/pstr.rs
Expand Up @@ -27,6 +27,11 @@ pub fn gen() -> TokenStream {
}
}
impl ::core::cmp::Eq for PSTR {}
impl ::core::fmt::Debug for PSTR {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple("PSTR").field(&self.0).finish()
}
}
unsafe impl ::windows::core::Abi for PSTR {
type Abi = Self;

Expand Down
5 changes: 5 additions & 0 deletions crates/libs/bindgen/src/replacements/pwstr.rs
Expand Up @@ -27,6 +27,11 @@ pub fn gen() -> TokenStream {
}
}
impl ::core::cmp::Eq for PWSTR {}
impl ::core::fmt::Debug for PWSTR {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple("PWSTR").field(&self.0).finish()
}
}
unsafe impl ::windows::core::Abi for PWSTR {
type Abi = Self;

Expand Down
36 changes: 36 additions & 0 deletions crates/libs/bindgen/src/structs.rs
Expand Up @@ -85,6 +85,7 @@ fn gen_struct_with_name(def: &TypeDef, struct_name: &str, cfg: &Cfg, gen: &Gen)

tokens.combine(&gen_struct_constants(def, &name, &cfg, gen));
tokens.combine(&gen_copy_clone(def, &name, &cfg, gen));
tokens.combine(&gen_debug(def, &name, &cfg, gen));
tokens.combine(&gen_windows_traits(def, &name, &cfg, gen));
tokens.combine(&gen_compare_traits(def, &name, &cfg, gen));

Expand Down Expand Up @@ -197,6 +198,41 @@ fn gen_compare_traits(def: &TypeDef, name: &TokenStream, cfg: &Cfg, gen: &Gen) -
}
}

fn gen_debug(def: &TypeDef, ident: &TokenStream, cfg: &Cfg, gen: &Gen) -> TokenStream {
if gen.sys || def.is_union() || def.has_explicit() || def.is_packed() {
quote! {}
} else {
let name = ident.as_str();
let cfg = cfg.gen(gen);

let fields = def.fields().map(|f| {
if f.is_literal() {
quote! {}
} else {
let name = f.name();
let ident = gen_ident(name);
let signature = f.signature(Some(def));
if signature.is_callback() {
quote! { .field(#name, &self.#ident.map(|f| f as usize)) }
} else if signature.is_callback_array() {
quote! {}
} else {
quote! { .field(#name, &self.#ident) }
}
}
});

quote! {
#cfg
impl ::core::fmt::Debug for #ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_struct(#name) #(#fields)* .finish()
}
}
}
}
}

fn gen_copy_clone(def: &TypeDef, name: &TokenStream, cfg: &Cfg, gen: &Gen) -> TokenStream {
let cfg = cfg.gen(gen);

Expand Down
7 changes: 7 additions & 0 deletions crates/libs/reader/src/element_type.rs
Expand Up @@ -194,6 +194,13 @@ impl ElementType {
}
}

pub fn is_callback_array(&self) -> bool {
match self {
Self::Array((kind, _)) => kind.kind.is_callback(),
_ => false,
}
}

pub fn is_primitive(&self) -> bool {
match self {
Self::TypeDef(t) => t.is_primitive(),
Expand Down
8 changes: 8 additions & 0 deletions crates/libs/reader/src/signature.rs
Expand Up @@ -34,6 +34,14 @@ impl Signature {
}
}

pub fn is_callback(&self) -> bool {
self.pointers == 0 && self.kind.is_callback()
}

pub fn is_callback_array(&self) -> bool {
self.pointers == 0 && self.kind.is_callback_array()
}

pub fn size(&self) -> usize {
if self.pointers > 0 {
1
Expand Down