Add support for custom per-field attributes#3307
Merged
emilio merged 2 commits intorust-lang:mainfrom Nov 1, 2025
Merged
Conversation
12b3e2a to
6554574
Compare
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.
6554574 to
5b9816f
Compare
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
emilio
reviewed
Oct 24, 2025
fa7c579 to
f2ef849
Compare
A single function gathers custom field attributes from cli, API and annotations, and then use it for both newtype and regular struct/union fields.
f2ef849 to
943d01a
Compare
Contributor
Author
|
@emilio Take another look? |
emilio
approved these changes
Oct 31, 2025
Contributor
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. |
Contributor
|
Yeah, that is due to a change in |
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>
Contributor
|
Done: #3309. |
github-merge-queue bot
pushed a commit
that referenced
this pull request
Nov 1, 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: #3307 [1] Link: rust-lang/rust#144443 [2] Link: Rust-for-Linux/linux@8851e27 [3] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds the ability to apply custom Rust attributes to individual
struct, union, and newtype fields in generated bindings through three
mechanisms:
--field-attr "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.