#macro-derive #expose #values #compile-time #defined #named #static

macro derive-enum-all-values

A procedural derive macro which exposes a method named all_values that returns all defined values as a compile-time static

1 unstable release

0.1.0 Sep 25, 2023

#1935 in Procedural macros

Custom license

6KB

derive-enum-all-values

Crates.io Docs.rs CI

About

A procedural derive macro which exposes a method named all_values that returns all defined values as a compile-time static.

Usage

Add derive_enum_all_values to your Cargo.toml:

[dependencies]
derive_enum_all_values = "0.1.0"

And then derive AllValues for your enum:

use derive_enum_all_values::AllValues;

#[derive(AllValues)]
enum MyEnum {
    Variant1,
    Variant2,
    // ... other variants ...
}

fn main() {
    for variant in MyEnum::all_values() {
        println!("{:?}", variant);
    }
}

This macro generates the following code:

impl MyEnum {
    pub fn all_values() -> &'static [MyEnum] {
        &[MyEnum::Variant1, MyEnum::Variant2]
    }
}

A static, compile time known array containing every enum value.

Development

How to develop this project.

Clone this Repository:

# When cloning, make sure symlinks are enabled
git clone -c core.symlinks=true https://github.com/Sewer56/derive-enum-all-values.git

Install Rust:

Visual Studio Code Integration

Code/VSCode is the de-facto Rust development environment.

The following extensions are required:

The VSCode configuration in Reloaded projects (.vscode) contain the following:

  • Run Rust linter clippy on Save.
  • Run code format rustfmt on Save.
  • Tasks for common operations (generate documentation, active CI/CD etc.).

These configurations are in the .vscode folder; and the tasks can be ran via Ctrl+Shift+P -> Run Task.

File Layout

The following is the expected file layout for your project:

.vscode/
src/
Cargo.toml

The src folder should contains all source code for your project.
Cargo.toml should be in the root of the project.

Contributing

See CONTRIBUTING for guidance on how to contribute to this project.

License

Licensed under MIT.

Dependencies

~275–730KB
~17K SLoC