Skip to content

Commit 68b9fb2

Browse files
arctic-alpacaemilio
authored andcommitted
Allow custom derives on new-type alias
1 parent f158981 commit 68b9fb2

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

bindgen-integration/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ impl ParseCallbacks for MacroCallback {
128128
vec!["PartialEq".into()]
129129
} else if info.name == "MyOrderedEnum" {
130130
vec!["std::cmp::PartialOrd".into()]
131+
} else if info.name == "TestDeriveOnAlias" {
132+
vec!["std::cmp::PartialEq".into(), "std::cmp::PartialOrd".into()]
131133
} else {
132134
vec![]
133135
}
@@ -193,6 +195,7 @@ fn setup_macro_test() {
193195
.blocklist_function("my_prefixed_function_to_remove")
194196
.constified_enum("my_prefixed_enum_to_be_constified")
195197
.opaque_type("my_prefixed_templated_foo<my_prefixed_baz>")
198+
.new_type_alias("TestDeriveOnAlias")
196199
.depfile(out_rust_file_relative.display().to_string(), &out_dep_file)
197200
.generate()
198201
.expect("Unable to generate bindings");

bindgen-integration/cpp/Test.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,6 @@ enum MyOrderedEnum {
241241
METER,
242242
LIGHTYEAR,
243243
};
244+
245+
// Used to test custom derives on new-type alias. See `test_custom_derive`.
246+
typedef int TestDeriveOnAlias;

bindgen-integration/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ fn test_custom_derive() {
289289

290290
assert!(meter < lightyear);
291291
assert!(meter > micron);
292+
293+
// The `add_derives` callback should have added `#[derive(PartialEq, PartialOrd)]`
294+
// to the `TestDeriveOnAlias` new-type alias. If it didn't, this will fail to compile.
295+
let test1 = unsafe { bindings::TestDeriveOnAlias(5) };
296+
let test2 = unsafe { bindings::TestDeriveOnAlias(6) };
297+
assert!(test1 < test2);
298+
assert!(!(test1 > test2));
292299
}
293300

294301
#[test]

bindgen/codegen/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,10 +1022,20 @@ impl CodeGenerator for Type {
10221022
let packed = false; // Types can't be packed in Rust.
10231023
let derivable_traits =
10241024
derives_of_item(item, ctx, packed);
1025-
if !derivable_traits.is_empty() {
1026-
let derives: Vec<_> = derivable_traits.into();
1027-
attributes.push(attributes::derives(&derives))
1028-
}
1025+
let mut derives: Vec<_> = derivable_traits.into();
1026+
// The custom derives callback may return a list of derive attributes;
1027+
// add them to the end of the list.
1028+
let custom_derives =
1029+
ctx.options().all_callbacks(|cb| {
1030+
cb.add_derives(&DeriveInfo {
1031+
name: &name,
1032+
kind: DeriveTypeKind::Struct,
1033+
})
1034+
});
1035+
// In most cases this will be a no-op, since custom_derives will be empty.
1036+
derives
1037+
.extend(custom_derives.iter().map(|s| s.as_str()));
1038+
attributes.push(attributes::derives(&derives));
10291039

10301040
quote! {
10311041
#( #attributes )*

0 commit comments

Comments
 (0)