#const #value #enum

macro enum_const_value

Providing const values for enums. Unit and enums with associated types are supported. Each variant gets a unique sequential value.

5 releases (3 breaking)

0.4.1 Aug 5, 2021
0.4.0 Aug 23, 2020
0.3.0 Aug 20, 2020
0.2.0 Aug 19, 2020
0.1.0 Aug 7, 2020

#388 in Rust patterns

Download history 166/week @ 2021-10-14 63/week @ 2021-10-21 43/week @ 2021-10-28 65/week @ 2021-11-04 114/week @ 2021-11-11 65/week @ 2021-11-18 12/week @ 2021-11-25 104/week @ 2021-12-02 209/week @ 2021-12-09 38/week @ 2021-12-16 49/week @ 2021-12-23 96/week @ 2021-12-30 33/week @ 2022-01-13

190 downloads per month

MIT/Apache

10KB
92 lines

Enum const value

Latest Version Build Status

A derive macro that will add const values for enum cases, even enums with associated types. When dealing with an enum with associated types, the macro creates a new enum with the const values.

Usage

[dependencies]
enum_const_value = "0.1.0"

Now in your project, add the following:

use enum_const_value::EnumConstValue;

and use the derive macro on an enum

#[derive(EnumConstValue)]
enum MyEnum {
    SomeI32(i32),
    SomeString(String)
}

Now, you can use use the const_value() method on an enum case like so:

#[test]
fn test_my_enum() {
    assert_eq!(0, MyEnum::SomeI32(1).const_value());
    assert_eq!(1, MyEnum::SomeString("MyString".to_string()).const_value());

    assert_eq!(0, MyEnumConstValue::SomeI32.value());
    assert_eq!(1, MyEnumConstValue::SomeString.value());
}

TODO

  • Custom enum case values (can be done by adding attributes)
  • Custom derived enum name (is currently always $ENUMNAME$ConstValue)

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~290–710KB
~18K SLoC