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 somevalue = MyEnum::Value8; let serialized = somevalue.toprimitive(); // tou32/other primitive types also works let isvaluediscriminant = MyEnum::isdiscriminant(serialized); let deserialized = MyEnum::fromprimitive(serialized); // panics if invalid value assert!(isvaluediscriminant); asserteq!(somevalue, deserialized); assert_eq!(serialized, 8); } ```