An inert attribute remains available during attribute processing instead of removing itself. In Rust derive macros, helper attributes are inert: the derive macro reads them to alter generated behavior and output, while the original input still retains them. section 8.1.1
This differs from an attribute macro. Attribute-macro annotations are active and remove themselves during processing, which is useful when the annotated item may disappear or be replaced. Derive macros cannot change their input, so retaining helper attributes is appropriate. section 8.7 section 8.1.1
Renaming a generated builder setter
A derive macro can use an inert helper attribute to customize generated code. Declaring `attributes(rename)` makes `rename` valid, while the macro implementation can generate the `tops_of` setter from the `roots_of` field: `#[derive(Builder)]` `struct Gleipnir {` ` #[rename("tops_of")]` ` roots_of: String,` `}` The test then calls `.tops_of("mountains".to_string())` and verifies that `roots_of` receives the value. Before the attribute is declared, compilation fails with an unknown-attribute error; after declaration, the remaining failure indicates that generation still needs implementation. section 8.1.1
Listing 8.1 Verifying the desired behavior with a test
#[test]
fn should_generate_builder_for_struct_with_one_renamed_property() {
#[derive(Builder)]
struct Gleipnir {
#[rename("tops_of")] #1
roots_of: String,
}
let gleipnir = Gleipnir::builder()
.tops_of("mountains".to_string()) #2
.build();
assert_eq!(
gleipnir.roots_of, #3
"mountains".to_string()
);
}
Inspect the complete test showing an inert `rename` helper attribute driving a generated builder method. section 8.1.1
