Skip to content

Commit 79437a5

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

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

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

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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

+10
Original file line numberDiff line numberDiff line change
@@ -1567,19 +1567,29 @@ 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
1571+
.annotations()
1572+
.attributes()
1573+
.iter()
1574+
.map(|s| s.parse().unwrap())
1575+
.collect::<Vec<_>>();
1576+
15701577
match visibility {
15711578
FieldVisibilityKind::Private => {
15721579
field.append_all(quote! {
1580+
#( #attributes )*
15731581
#field_ident : #ty ,
15741582
});
15751583
}
15761584
FieldVisibilityKind::PublicCrate => {
15771585
field.append_all(quote! {
1586+
#( #attributes )*
15781587
pub(crate) #field_ident : #ty ,
15791588
});
15801589
}
15811590
FieldVisibilityKind::Public => {
15821591
field.append_all(quote! {
1592+
#( #attributes )*
15831593
pub #field_ident : #ty ,
15841594
});
15851595
}

bindgen/ir/annotations.rs

+1-1
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)