Crates.io | light_enum |
lib.rs | light_enum |
version | 0.2.2 |
source | src |
created_at | 2023-10-31 03:10:35.39668 |
updated_at | 2023-11-15 16:52:20.295364 |
description | provide a derive keyword to generate a light enum |
homepage | |
repository | https://github.com/wiiznokes/light_enum |
max_upload_size | |
id | 1019319 |
size | 10,354 |
A crate providing a derive keyword to generate a light enum.
cargo add light_enum
This crate provide two derive keywords:
LightEnum
will generate a new enum without the content of each fieldValues
will generate a vector containing each field of the enumSee examples bellow to understand what they do.
use light_enum::LightEnum;
#[derive(LightEnum)]
enum MyEnum {
A(i32, i32),
B(i32),
C,
}
let heavy = MyEnum::A(0, 0);
let light = heavy.to_light();
assert!(light == MyEnumLight::A);
MyEnumLight
will be generated:
enum MyEnumLight {
A,
B,
C,
}
use light_enum::Values;
#[derive(Values, PartialEq, Eq)]
enum Vals {
A,
B,
C,
}
let values = Vals::VALUES;
assert!(values.len() == 3);
assert!(values.contains(&Vals::A));
assert!(values.contains(&Vals::B));
assert!(values.contains(&Vals::C));
Generated code:
impl Vals {
const VALUES: [Vals; 3usize] = [Vals::A, Vals::B, Vals::C];
}
Having a field with content will cause an error.
// this code will not compile
#[derive(Values)]
enum MyEnum {
A(i32),
}
The cargo expand
utility is useful to see what is generated. It can be installed with cargo install cargo-expand
.