Show field attributes in documentation

Hello,

I have a naive question about documentation of field attributes.

I have a struct like this where I derived some modbus-related trait which uses field attributes. (Although, the modbus stuff is not that relevant for the question.)

#[derive(MyModbus)]
struct InputRegister {
    #[my_modbus(address = 1)
    power: f32,
}

When I run cargo doc and check the documentation I see

struct InputRegister {
    power: f32,
}

and documentation for MyModbus trait. For my_modbus field attribute, the reader needs to click on the source hyperlink.

Is there a way to display the line #[my_modbus(address = 1) in the struct documentation?

You can make your macro generate doc comments.

#[doc = "this has address = 1"]
pub power: f32,

This will be shown in the "Fields" section of the documentation, under power. If power isn't pub, it won't show up anywhere.

Another good place to put a doc comment is in the generated implementation of MyModbus, although these have much lower visibility in the documentation than struct fields.

I don't think there's any way to change the struct representation in the documentation, besides changing the struct itself. Macro attributes don't (necessarily) change the struct, so they aren't of any concern to the documentation, which mostly only cares about the API.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.