Skip to content

Commit 1a45431

Browse files
committed
Add proper Debug implementation printing the fields one by one
1 parent c739fd5 commit 1a45431

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

macros/src/generator.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,44 @@ pub fn expand_bitmap(input: BitmapInput) -> syn::Result<TokenStream2> {
5454
}
5555
});
5656

57+
let debug_fields = fields.iter().map(|ident| {
58+
let field_name = &ident.name;
59+
let field_name_str = field_name.to_string();
60+
quote! {
61+
.field(#field_name_str, &self.#field_name())
62+
}
63+
});
64+
65+
let name_str = name.to_string();
66+
5767
Ok(quote! {
58-
#[derive(Debug, Clone, Copy)]
68+
#[derive(Clone, Copy)]
5969
#[repr(transparent)]
60-
pub struct #name(#storage_ty);
70+
pub struct #name(#storage_ty);
6171

62-
impl #name {
63-
#(#accessors)*
64-
}
72+
impl #name {
73+
#(#accessors)*
74+
}
6575

66-
impl ::core::convert::From<#name> for #storage_ty {
67-
fn from(value: #name) -> Self {
68-
value.0
69-
}
70-
}
76+
impl ::core::convert::From<#name> for #storage_ty {
77+
fn from(value: #name) -> Self {
78+
value.0
79+
}
80+
}
81+
82+
impl ::core::ops::Deref for #name {
83+
type Target = #storage_ty;
84+
fn deref(&self) -> &Self::Target {
85+
&self.0
86+
}
87+
}
7188

72-
impl ::core::ops::Deref for #name {
73-
type Target = #storage_ty;
74-
fn deref(&self) -> &Self::Target {
75-
&self.0
76-
}
77-
}
89+
impl ::core::fmt::Debug for #name {
90+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
91+
f.debug_struct(#name_str)
92+
#(#debug_fields)*
93+
.finish()
94+
}
95+
}
7896
})
7997
}

0 commit comments

Comments
 (0)