Crates.io | enum_const_value |
lib.rs | enum_const_value |
version | 0.4.3 |
source | src |
created_at | 2020-08-07 10:34:39.098519 |
updated_at | 2023-09-25 15:31:25.855216 |
description | Providing const values for enums. Unit and enums with associated types are supported. Each variant gets a unique sequential value. |
homepage | |
repository | https://github.com/jasperav/enum_const_value |
max_upload_size | |
id | 273934 |
size | 30,243 |
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.
[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());
}