liveBook · Manning

Active attribute

As explained in Write Powerful Rust Macros

An active attribute is an attribute that is removed during attribute processing after its macro expands.

An active attribute belongs to an attribute macro and removes itself during attribute processing after the macro expands. Derive macro attributes are inert instead. section 8.7

Removal matters when an attribute macro emits no replacement item: keeping the annotation would leave an attribute attached to something that has disappeared. section 8.7

A flexible attribute macro

The invocation #[public(exclude(fourth, third))] carries custom properties to an attribute macro. The macro can make selected struct fields public while leaving excluded fields unchanged; the active annotation is then consumed during processing. section 8.7

Listing 8.34 example.rs, with a struct
use make_public_macro::public;

#[public(exclude(fourth, third))]        #1
struct Example {
    first: String,
    pub second: u32,
    third: bool,
    fourth: String,
}

impl Example {
    pub fn new() -> Self { #2
        Example {
            first: "first".to_string(),
            // etc.
        }
    }                 
}

Inspect the attribute-macro invocation and its excluded-field configuration. section 8.7

How Write Powerful Rust Macros covers it

How deep it goes
Defines removal and contrasts it with inert derive attributes, stopping short of specifying compiler processing internals.
Examples
Uses a public-fields macro with an exclude property and a struct whose generated visibility can be checked by compilation.
What you can do after
Evaluate whether an attribute should remain after an attribute macro transforms or removes its attached item.
How it connects
Connects attribute macros with derive macros, procedural macros, attribute processing, and abstract syntax trees.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.