6 stable releases
1.1.1 | Jan 12, 2022 |
---|---|
1.0.3 | Apr 9, 2020 |
1.0.2 | Apr 8, 2020 |
1.0.1 | Feb 27, 2020 |
#96 in #enum
25 downloads per month
Used in 2 crates
(via basin2-data)
16KB
143 lines
Cenum
Purpose
Cenum (C + Enum) is a small Rust crate that is a spiritual successor to the enum_primitive
crate. It uses an attribute macro over a given enum to allow low-cost conversion from a field-less enum (discriminants are allowed) to a usize
and back again.
Usage
use cenum::{ cenum, Cenum }; // exposed trait is Cenum
#[cenum]
enum MyEnum {
Value0,
Value1,
Value7 = 7,
Value8,
Value9,
}
fn test() {
let some_value = MyEnum::Value8;
let serialized = some_value.to_primitive(); // to_u32/other primitive types also works
let is_value_discriminant = MyEnum::is_discriminant(serialized);
let deserialized = MyEnum::from_primitive(serialized); // panics if invalid value
assert!(is_value_discriminant);
assert_eq!(some_value, deserialized);
assert_eq!(serialized, 8);
}
Dependencies
~1.5MB
~36K SLoC