Attribute style guidelines: `#[key(value)]` vs. `#[key = value]`

TL;DR: do you prefer #[key(value)] or #[key = value] when value is a single expression?


Hello!

I'm designing a proc_macro_attribute that accepts a struct item in which fields may be decorated with various custom outer attributes. An invocation might look like:

#[my_proc_macro_attribute]
struct S {
    #[my_field_attribute]
    a: u32,
}

One of these field attributes, my_field_attribute, represents a quantity that is given by either an explicit expression or a default value.

I have chosen the syntax #[my_field_attribute] to invoke a default value, but the syntax to pass an expression is yet undecided. From my perspective, there are three options:

  1. #[my_field_attribute(expr)]
  2. #[my_field_attribute = expr]
  3. Both 1 and 2

I have searched the Reference, rustc dev guide, and other posts on the users and internals forums, but I could not find any opinions on the matter.

By my analysis, Option 1 is more flexible because it accepts a delimited sequence of token trees (which is more general than an expression), but Option 2 has conventional key-value semantics (which may be more intuitive if the quantity that my_field_attribute represents can logically have no other properties besides its singular value).

Does anyone have an opinion on this? (Or am I just overthinking it?)

serde does #[serde(key = value)] for inner attributes, which I find nice.
https://serde.rs/container-attrs.html

4 Likes

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.