Skip to content

Commit 9a91261

Browse files
committed
feat: add field attributes support
Signed-off-by: Leonardo Di Giovanna <[email protected]>
1 parent 97ab915 commit 9a91261

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

bindgen-tests/tests/expectations/tests/field_attributes.rs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
struct S {
2+
/// <div rustbindgen attribute="#[Attr1] #[Attr2]" attribute="#[Attr3(value)]"></div>
3+
int field_1;
4+
char field_2;
5+
};

bindgen/codegen/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,19 +1567,27 @@ impl FieldCodegen<'_> for FieldData {
15671567
let accessor_kind =
15681568
self.annotations().accessor_kind().unwrap_or(accessor_kind);
15691569

1570+
let attributes: Vec<proc_macro2::TokenStream> = self.annotations()
1571+
.attributes()
1572+
.iter()
1573+
.map(|s| s.parse().unwrap()).collect::<Vec<_>>();
1574+
15701575
match visibility {
15711576
FieldVisibilityKind::Private => {
15721577
field.append_all(quote! {
1578+
#( #attributes )*
15731579
#field_ident : #ty ,
15741580
});
15751581
}
15761582
FieldVisibilityKind::PublicCrate => {
15771583
field.append_all(quote! {
1584+
#( #attributes )*
15781585
pub(crate) #field_ident : #ty ,
15791586
});
15801587
}
15811588
FieldVisibilityKind::Public => {
15821589
field.append_all(quote! {
1590+
#( #attributes )*
15831591
pub #field_ident : #ty ,
15841592
});
15851593
}

bindgen/ir/annotations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) struct Annotations {
102102
constify_enum_variant: bool,
103103
/// List of explicit derives for this type.
104104
derives: Vec<String>,
105-
/// List of explicit attributes for this type.
105+
/// List of explicit attributes for this type/field.
106106
attributes: Vec<String>,
107107
}
108108

0 commit comments

Comments
 (0)