Allow specifying attributes in impl mode #406

Closed
opened 2024-05-15 00:20:24 -05:00 by madsmtm · 1 comment
madsmtm commented 2024-05-15 00:20:24 -05:00 (Migrated from github.com)

I'm considering cfg-gating the use of bitflags! in my libraries, and to that end it would be nice if it was possible to apply attributes to the generated impls as well.

The naive solution would be:

pub struct Flags(u8);

#[cfg(feature = "bitflags")]
bitflags! {
    impl Flags: u8 {
        // ...
    }
}

However, that works poorly with the doc_cfg (and doc_auto_cfg) feature, as the attribute is only applied to the macro, and isn't passed down to each output item.

Instead, I would like to be able to do the following:

#![feature(doc_cfg)]

pub struct Flags(u8);

#[cfg(feature = "bitflags")]
bitflags! {
    #[doc(cfg(feature = "bitflags"))]
    impl Flags: u8 {
        // ...
    }
}
I'm considering `cfg`-gating the use of `bitflags!` in my libraries, and to that end it would be nice if it was possible to apply attributes to the generated `impl`s as well. The naive solution would be: ```rust pub struct Flags(u8); #[cfg(feature = "bitflags")] bitflags! { impl Flags: u8 { // ... } } ``` However, that works poorly with the `doc_cfg` (and `doc_auto_cfg`) feature, as the attribute is only applied to the macro, and isn't passed down to each output item. Instead, I would like to be able to do the following: ```rust #![feature(doc_cfg)] pub struct Flags(u8); #[cfg(feature = "bitflags")] bitflags! { #[doc(cfg(feature = "bitflags"))] impl Flags: u8 { // ... } } ```
KodrAus commented 2024-06-16 16:38:49 -05:00 (Migrated from github.com)

Thanks for the report @madsmtm! Anywhere attributes are allowed to be specified we should support in the macros as much as possible. I think this is just a missing piece of functionality for the impl mode.

Thanks for the report @madsmtm! Anywhere attributes are allowed to be specified we should support in the macros as much as possible. I think this is just a missing piece of functionality for the impl mode.
Sign in to join this conversation.
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags#406
No description provided.