File tree 4 files changed +27
-4
lines changed 4 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -128,6 +128,8 @@ impl ParseCallbacks for MacroCallback {
128
128
vec ! [ "PartialEq" . into( ) ]
129
129
} else if info. name == "MyOrderedEnum" {
130
130
vec ! [ "std::cmp::PartialOrd" . into( ) ]
131
+ } else if info. name == "TestDeriveOnAlias" {
132
+ vec ! [ "std::cmp::PartialEq" . into( ) , "std::cmp::PartialOrd" . into( ) ]
131
133
} else {
132
134
vec ! [ ]
133
135
}
@@ -193,6 +195,7 @@ fn setup_macro_test() {
193
195
. blocklist_function ( "my_prefixed_function_to_remove" )
194
196
. constified_enum ( "my_prefixed_enum_to_be_constified" )
195
197
. opaque_type ( "my_prefixed_templated_foo<my_prefixed_baz>" )
198
+ . new_type_alias ( "TestDeriveOnAlias" )
196
199
. depfile ( out_rust_file_relative. display ( ) . to_string ( ) , & out_dep_file)
197
200
. generate ( )
198
201
. expect ( "Unable to generate bindings" ) ;
Original file line number Diff line number Diff line change @@ -241,3 +241,6 @@ enum MyOrderedEnum {
241
241
METER,
242
242
LIGHTYEAR,
243
243
};
244
+
245
+ // Used to test custom derives on new-type alias. See `test_custom_derive`.
246
+ typedef int TestDeriveOnAlias;
Original file line number Diff line number Diff line change @@ -289,6 +289,13 @@ fn test_custom_derive() {
289
289
290
290
assert ! ( meter < lightyear) ;
291
291
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) ) ;
292
299
}
293
300
294
301
#[ test]
Original file line number Diff line number Diff line change @@ -1022,10 +1022,20 @@ impl CodeGenerator for Type {
1022
1022
let packed = false ; // Types can't be packed in Rust.
1023
1023
let derivable_traits =
1024
1024
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) ) ;
1029
1039
1030
1040
quote ! {
1031
1041
#( #attributes ) *
You can’t perform that action at this time.
0 commit comments