rust-x-bindings / rust-xcb

Rust bindings and wrapper for XCB.
MIT License
165 stars 64 forks source link

Allow attributes on fields inside `atoms_struct!` #169

Closed RealKC closed 2 years ago

RealKC commented 2 years ago

The main usecase this enables is putting doc comments on the fields as they get desugared to #[doc="..."]-style attributes


I had a struct in my app that could use this macro but needed this feature, so I decided to add it myself

rtbo commented 2 years ago

Great! Can you add an example of such attribute for one of the fields in the rustdoc example of this macro? FYI all examples are tested to compile in the github workflows.

RealKC commented 2 years ago

I updated the current example, is this good or should I go for something else?

rtbo commented 2 years ago

I didn't understand you were referring to regular doc comments. It is not possible to put such comments without this PR? What about regular comments?

RealKC commented 2 years ago

I didn't understand you were referring to regular doc comments. It is not possible to put such comments without this PR?

Yes, doc comments get desugared by the rust compiler into a #[doc] attribute on the item, i.e.

struct Foo {
    /// Doc comment
    pub field: i32,
}

becomes

struct Foo {
    #[doc = "Doc comment"]
    pub field: i32,
}

It's also mentioned in the rust reference

What about regular comments?

I believe they don't get passed to macro_rules! macros (the page I linked above says they get interpreted as a form of whitespace)

rtbo commented 2 years ago

OK, thanks for the explanations!