feat(core): Add bitflags_match macro for bitflag matching#423
feat(core): Add bitflags_match macro for bitflag matching#423KodrAus merged 4 commits intobitflags:mainfrom
Conversation
Implemented a new macro, `match_bitflag`, to perform matching operations on bitflags, similar to Rust's match expression. This macro addresses the matching issues that arise when using regular match expressions with bitflags. - The macro supports complex bitflag combination matching. - Added unit tests to verify the correctness of the macro. - Provided usage examples and an explanation of the macro's internal implementation.
|
Hi @YuniqueUnic 👋 It's a bit of a papercut that Would you be happy to rename this to match x {
y => {} // <-- this is a block, so no trailing comma needed here
z => (), // <-- this is not a block, so trailing comma needed here
} |
- 将 `match_bitflag!` 宏重命名为 `bitflags_match!`,以更好地反映其功能 - 更新了 `bitflags_match!` 宏的文档说明,提高了清晰度 - 修改了相关测试模块的名称,以适应新的宏命名
|
Hello, @KodrAus Thanks for the feedback! I've made the changes:
I tried to make the syntax follow the match arm rules (blocks do not require a trailing comma, non-blocks do), but it failed to compile, so for now, each pattern in the match still requires a trailing comma... I'm unable to solve this problem... Please let me know if there are any other adjustments needed! |
|
Thanks @YuniqueUnic! I think we'll need to make trailing commas work correctly before we can ship this, but it may require some macro-hackery to make it work. I'd be happy to try take a look at it and see if I can come up with a solution. |
|
I've had a look at this and come up with a macro that I think matches most of the standard What I've written is a style of macro called a tt-muncher. It lets us incrementally pull tokens and expand them as we go. The main benefit it gives us is a way to apply multiple rules to a collection of inputs. So we can have a rule for match arms where the body is surrounded by You should be able to pull this commit or my https://github.com/KodrAus/bitflags/tree/feat/bitflags_match branch into this PR. |
…flags_match` macro - Thanks to the help from https://github.com/KodrAus, the syntax of `bitflags_match` is aligned with Rust's `match` syntax. - Support block statements as the body of match arms, which improves code expressiveness and readability. - Added handling for trailing commas, making the use of the macro more flexible. - Optimized the internal implementation of the macro so that it expands to a series of `if(pattern){ return result; }` statements. - Updated the documentation to clarify the usage and precautions of the macro.
|
Thank you for your support!!! I forked your code, modified the doc of Thanks!!
|
KodrAus
left a comment
There was a problem hiding this comment.
Thanks @YuniqueUnic! This looks good to me.
I encountered an “unreachable” issue while utilizing Rust’s match statement. In search of a more effective solution, I considered employing a macro. However, I found that there wasn’t an existing macro to address this issue. Consequently, I decided to create one myself.
Could you please review my code? I’m looking for feedback and suggestions. If you find it beneficial and suitable, I would appreciate it if you could merge it. Thank you.
related issues:
Implemented a new macro,
match_bitflag, to perform matching operations on bitflags, similar to Rust's match expression. This macro addresses the matching issues that arise when using regular match expressions with bitflags.