Skip to content

Constant values for enums with associated types

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

Jasperav/enum_const_value

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Constant values for enums with associated types

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages