Skip to content

Commit

Permalink
replace #[derive(Debug)] with manual impls
Browse files Browse the repository at this point in the history
The new impls should be identical, but they have the advantage that we
can move them around. See the next patch for why this is useful.
  • Loading branch information
Freax13 committed Nov 30, 2024
1 parent 9bf993b commit 5f9ce73
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions derive/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,19 +607,24 @@ fn generate_checked_bit_pattern_struct(
let field_name = &field_names[..];
let field_ty = &field_tys[..];

let derive_dbg =
quote!(#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]);

Ok((
quote! {
#[doc = #GENERATED_TYPE_DOCUMENTATION]
#repr
#[derive(Clone, Copy, #crate_name::AnyBitPattern)]
#derive_dbg
#[allow(missing_docs)]
pub struct #bits_ty {
#(#field_name: <#field_ty as #crate_name::CheckedBitPattern>::Bits,)*
}

#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #bits_ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty));
#(::core::fmt::DebugStruct::field(&mut debug_struct, ::core::stringify!(#field_name), &self.#field_name);)*
::core::fmt::DebugStruct::finish(&mut debug_struct)
}
}
},
quote! {
type Bits = #bits_ty;
Expand Down Expand Up @@ -711,9 +716,6 @@ fn generate_checked_bit_pattern_enum_with_fields(
let representation = get_repr(&input.attrs)?;
let vis = &input.vis;

let derive_dbg =
quote!(#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]);

match representation.repr {
Repr::Rust => unreachable!(),
repr @ (Repr::C | Repr::CWithDiscriminant(_)) => {
Expand Down Expand Up @@ -793,13 +795,22 @@ fn generate_checked_bit_pattern_enum_with_fields(
quote! {
#[doc = #GENERATED_TYPE_DOCUMENTATION]
#[derive(::core::clone::Clone, ::core::marker::Copy, #crate_name::AnyBitPattern)]
#derive_dbg
#bits_repr
#vis struct #bits_ty_ident {
tag: #integer,
payload: #variants_union_ident,
}

#[cfg(not(target_arch = "spirv"))]
impl ::core::fmt::Debug for #bits_ty_ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut debug_struct = ::core::fmt::Formatter::debug_struct(f, ::core::stringify!(#bits_ty_ident));
::core::fmt::DebugStruct::field(&mut debug_struct, "tag", &self.tag)?;
::core::fmt::DebugStruct::field(&mut debug_struct, "payload", &self.payload);
::core::fmt::DebugStruct::finish(&mut debug_struct)
}
}

#[derive(::core::clone::Clone, ::core::marker::Copy, #crate_name::AnyBitPattern)]
#[repr(C)]
#[allow(non_snake_case)]
Expand Down

0 comments on commit 5f9ce73

Please sign in to comment.