Crates.io | cenum |
lib.rs | cenum |
version | 1.1.1 |
source | src |
created_at | 2020-02-27 04:15:50.937084 |
updated_at | 2022-01-12 15:36:12.597357 |
description | A rust derive macro for C-style primitive enums. |
homepage | |
repository | https://github.com/Protryon/cenum |
max_upload_size | |
id | 212957 |
size | 44,540 |
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.
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);
}