-
Couldn't load subscription status.
- Fork 39
Open
Description
Goal
- We're adding a
#[derive(EnumCount)]macro toderive_tools. - It will stick a
const COUNT: usizeon enums, telling you how many variants they have. The nameCOUNTcan be changed with#[enum_count(const_name = "YOUR_NAME_HERE")]. - This macro is strictly for enums; it should cause a compile error if used on anything else.
- We'll also add an
#[enum_count(debug)]option to print out the code it generates, which is handy for development. - This new derive needs to be wired up in
derive_tools_meta(where the magic happens) andderive_tools(what users see), including a new feature flagderive_enum_count. - Naturally, it needs good tests (manual vs. derived, and tests that check for compile errors) and documentation.
- We'll stick to the
wToolsway of doing proc macros.
Key Requirements
- Functionality: Count variants for all enum types; error if not an enum.
- Attributes:
#[enum_count(debug)],#[enum_count(const_name = "CUSTOM_NAME")]. - Integration: Implement in
derive_tools_meta, expose viaderive_tools. Addderive_enum_countfeature flag. Updatebuild.rs. - Testing: Full tests (manual, derive, shared, compile-fail) in
derive_tools/tests/inc/enum_count/. - Docs: Rustdoc, Readme, examples.
- Branching: All development work and new branches for this task must originate from the
alphabranch of theWandalen/wToolsrepository.
Expected Behavior (EnumCount Macro)
- E1: For
enum MyEnum { A, B }, it generatesimpl MyEnum { pub const COUNT: usize = 2; }. - E2: For
enum MyEnum {}, it generatesimpl MyEnum { pub const COUNT: usize = 0; }. - E3: With
#[enum_count(const_name = "TOTAL_VARIANTS")] enum MyEnum { X }, it generatesimpl MyEnum { pub const TOTAL_VARIANTS: usize = 1; }. - E4: If used on
struct MyStruct;, it must cause a compile-time error. - E5: Using
#[enum_count(debug)]will print the generated code during compilation. - E6: It should correctly count variants for simple enums, those with data (tuples/structs), and generic enums.
Proc Macro Development Workflow (Mandatory)
Stick to the wTools "Proc Macro: Development Workflow" design rule. For each test case or significant feature variation:
- Manual Code: First, write the code you expect the macro to generate by hand. Put this in
tests/inc/enum_count/*_manual.rs. - Shared Test: Create a corresponding
*_only_test.rsfile. This will have the actual#[test]functions that check if theCOUNT(or custom named) constant is correct. - Test Manual:
include!the shared test in your manual file. Get the user to run it and confirm it passes. This proves your target output is correct. - Derive Site: Create a
*_derive.rsfile. Use the same enum structure, but this time apply#[derive(EnumCount)]. Alsoinclude!the shared test here. - Implement Macro: Now, write or update the
EnumCountmacro logic inderive_tools_metaso it generates code that matches your validated manual version. - Verify: Ask the user to run tests for both the manual and derive versions. If the derive version fails but the manual one passed, print the macro's output (using
debugif needed) and compare it to your manual code to find the bug. - Compile-Fail Tests: For things like using
EnumCounton a struct, add tests intests/inc/enum_count/compiletime/that are supposed to fail compilation. This checks your error handling.
Metadata
Metadata
Assignees
Labels
No labels