Skip to content
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions bindgen-tests/tests/headers/issue-3268-newtype-deref-union-debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// bindgen-flags: --impl-debug --default-alias-style=new_type_deref

union Union {
unsigned char bytes[4];
unsigned int word;
};

typedef union Union UnionAlias;

struct StructContainingUnionAlias {
UnionAlias ua;
};
18 changes: 18 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ impl CodeGenerator for Type {
};

let alias_style = item.alias_style(ctx);
let mut needs_debug_impl = false;

// We prefer using `pub use` over `pub type` because of:
// https://github.com/rust-lang/rust/issues/26264
Expand Down Expand Up @@ -1107,6 +1108,12 @@ impl CodeGenerator for Type {
let packed = false; // Types can't be packed in Rust.
let derivable_traits =
derives_of_item(item, ctx, packed);
if !derivable_traits.contains(DerivableTraits::DEBUG) {
needs_debug_impl = ctx.options().derive_debug &&
ctx.options().impl_debug &&
!ctx.no_debug_by_name(item) &&
!item.annotations().disallow_debug();
}
let mut derives: Vec<_> = derivable_traits.into();
// The custom derives callback may return a list of derive attributes;
// add them to the end of the list.
Expand Down Expand Up @@ -1238,6 +1245,17 @@ impl CodeGenerator for Type {
});
}

if needs_debug_impl {
let prefix = ctx.trait_prefix();
tokens.append_all(quote! {
impl ::#prefix::fmt::Debug for #rust_name {
fn fmt(&self, f: &mut ::#prefix::fmt::Formatter<'_>) -> ::#prefix::fmt::Result {
f.debug_tuple(stringify!(#rust_name)).field(&self.0).finish()
}
}
});
}

result.push(tokens);
}
TypeKind::Enum(ref ei) => ei.codegen(ctx, result, item),
Expand Down
Loading