Add ability to rename flags. #470

Closed
opened 2025-11-18 10:29:29 -06:00 by WesleyAC · 1 comment
WesleyAC commented 2025-11-18 10:29:29 -06:00 (Migrated from github.com)

Various functions allow interacting with the names of flags as strings (Flag::name, parser, etc). However, these currently must work with the SCREAMING_SNAKE_CASE name of the flag as it appears in the source code, which is not always desirable.

In Arti, we use bitflags for our router status flags, which have specific names assigned to them that do not match the Rust identifiers. It would be convenient to have a way of renaming flags with a attribute macro — here's where this usecase came up.

Various functions allow interacting with the names of flags as strings ([`Flag::name`](https://docs.rs/bitflags/latest/bitflags/struct.Flag.html#method.name), [`parser`](https://docs.rs/bitflags/latest/bitflags/parser/index.html), etc). However, these currently must work with the `SCREAMING_SNAKE_CASE` name of the flag as it appears in the source code, which is not always desirable. In [Arti](https://arti.torproject.org), we use bitflags for our [router status flags](https://spec.torproject.org/dir-spec/consensus-formats.html#item:s), which have specific names assigned to them that do not match the Rust identifiers. It would be convenient to have a way of renaming flags with a attribute macro — [here's where this usecase came up](https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/3454/diffs#diff-content-4c8b4a5b821efe6b52d4f98dfd87303c58db3166).
KodrAus commented 2025-11-22 16:22:13 -06:00 (Migrated from github.com)

Thanks for the suggestion @WesleyAC 👍 I'm generally against adding custom attributes to the bitflags macros because the macro_rules macros are already mostly busting at the seams.

In this case though, I think it would be reasonable to support through an attribute since there's no other way around the issue. We would basically need to find any places we call stringify! and replace them with another macro call that also accepted the attributes on the flag and figured out what its actual name should be.

Thanks for the suggestion @WesleyAC :+1: I'm generally against adding custom attributes to the `bitflags` macros because the `macro_rules` macros are already mostly busting at the seams. In this case though, I think it would be reasonable to support through an attribute since there's no other way around the issue. We would basically need to find any places we call `stringify!` and replace them with another macro call that also accepted the attributes on the flag and figured out what its actual name should be.
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#470
No description provided.