Skip to content

Conversation

@jsgf
Copy link
Contributor

@jsgf jsgf commented Oct 22, 2025

This adds the ability to apply custom Rust attributes to individual
struct, union, and newtype fields in generated bindings through three
mechanisms:

  1. HTML annotations in C/C++ comments:
/// <div rustbindgen attribute="serde(rename = x_coord)"></div>
int x;
  1. ParseCallbacks::field_attributes() method for programmatic control:
fn field_attributes(&self, info: &FieldAttributeInfo) -> Vec<String>
  1. CLI flag and Builder API for pattern-based attributes:
--field-attr "Point::x=serde(rename = x_coord)"
.field_attribute("Point", "x", "serde(rename = x_coord)")

All three mechanisms can be used together, with attributes merged in
order: annotations, callbacks, then CLI/Builder patterns.

This is useful for adding serde attributes, documentation, or other
derive-related metadata to specific fields.

@jsgf jsgf force-pushed the custom-field-attrs branch 3 times, most recently from 12b3e2a to 6554574 Compare October 22, 2025 02:21
@jsgf jsgf changed the title feat: Add support for custom per-field attributes Add support for custom per-field attributes Oct 22, 2025
This adds the ability to apply custom Rust attributes to individual
struct, union, and newtype fields in generated bindings through three
mechanisms:

1. HTML annotations in C/C++ comments:
```c
/// <div rustbindgen attribute="serde(rename = x_coord)"></div>
int x;
```

2. ParseCallbacks::field_attributes() method for programmatic control:
```rust
fn field_attributes(&self, info: &FieldAttributeInfo) -> Vec<String>
```

3. CLI flag and Builder API for pattern-based attributes:
```bash
--field-attr "Point::x=serde(rename = x_coord)"
```
```rust
.field_attribute("Point", "x", "serde(rename = x_coord)")
```

All three mechanisms can be used together, with attributes merged in
order: annotations, callbacks, then CLI/Builder patterns.

This is useful for adding serde attributes, documentation, or other
derive-related metadata to specific fields.
@jsgf jsgf force-pushed the custom-field-attrs branch from 6554574 to 5b9816f Compare October 22, 2025 02:23
meta-codesync bot pushed a commit to facebook/sapling that referenced this pull request Oct 23, 2025
Summary:
This includes 3 recent PRs:
- rust-lang/rust-bindgen#3305 - Fix packed structs with flexible array members (merged)
- rust-lang/rust-bindgen#3306 - Support nested flexible array members
- rust-lang/rust-bindgen#3307 - Add support for custom per-field attributes

The last one is specifically intended to allow per-field attributes from proc-macros to be applied.

These have been cherry-picked onto the v0.72.1 release branch, as the main branch is still listed as v0.72.0 and I didn't want a backwards version step. The upstream branch is https://github.com/jsgf/rust-bindgen/tree/PR-merged

Reviewed By: dtolnay

Differential Revision: D85273915

fbshipit-source-id: 211916b9611cbfa409296a6854b4a94bba2c5195
meta-codesync bot pushed a commit to facebookexperimental/rust-shed that referenced this pull request Oct 23, 2025
Summary:
This includes 3 recent PRs:
- rust-lang/rust-bindgen#3305 - Fix packed structs with flexible array members (merged)
- rust-lang/rust-bindgen#3306 - Support nested flexible array members
- rust-lang/rust-bindgen#3307 - Add support for custom per-field attributes

The last one is specifically intended to allow per-field attributes from proc-macros to be applied.

These have been cherry-picked onto the v0.72.1 release branch, as the main branch is still listed as v0.72.0 and I didn't want a backwards version step. The upstream branch is https://github.com/jsgf/rust-bindgen/tree/PR-merged

Reviewed By: dtolnay

Differential Revision: D85273915

fbshipit-source-id: 211916b9611cbfa409296a6854b4a94bba2c5195
@jsgf jsgf force-pushed the custom-field-attrs branch from fa7c579 to f2ef849 Compare October 24, 2025 18:41
A single function gathers custom field attributes from cli, API and
annotations, and then use it for both newtype and regular struct/union
fields.
@jsgf jsgf force-pushed the custom-field-attrs branch from f2ef849 to 943d01a Compare October 24, 2025 18:42
@jsgf
Copy link
Contributor Author

jsgf commented Oct 31, 2025

@emilio Take another look?

@emilio emilio added this pull request to the merge queue Oct 31, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 31, 2025
@emilio
Copy link
Contributor

emilio commented Oct 31, 2025

error: error loading target specification: target-pointer-width: invalid type: string "64", expected u16 at line 8 column 32

I think that's the rust-for-linux test, cc @ojeda

I don't think the issue is caused by this PR in any case.

@ojeda
Copy link
Contributor

ojeda commented Oct 31, 2025

Yeah, that is due to a change in rustc in 1.91.0. I added support for that in mainline a while ago: upgrading the Linux version to >= v6.17-rc5 should fix it -- I will send a quick PR.

ojeda added a commit to ojeda/rust-bindgen that referenced this pull request Oct 31, 2025
This upgrade is also needed now since Rust 1.91.0 got released, which
blocks the CI [1] due to the change in the target spec format in upstream
Rust [2]. It was fixed in Linux v6.17-rc5 [3].

Link: rust-lang#3307 [1]
Link: rust-lang/rust#144443 [2]
Link: Rust-for-Linux/linux@8851e27 [3]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
ojeda added a commit to ojeda/rust-bindgen that referenced this pull request Oct 31, 2025
This upgrade is also needed now since Rust 1.91.0 got released, which
blocks the CI [1] due to the change in the target spec format in upstream
Rust [2]. Support for the new format was added in Linux v6.17-rc5 [3].

Link: rust-lang#3307 [1]
Link: rust-lang/rust#144443 [2]
Link: Rust-for-Linux/linux@8851e27 [3]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
@ojeda
Copy link
Contributor

ojeda commented Oct 31, 2025

Done: #3309.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants