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
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.

1 participant