cenum

Crates.iocenum
lib.rscenum
version1.1.1
sourcesrc
created_at2020-02-27 04:15:50.937084
updated_at2022-01-12 15:36:12.597357
descriptionA rust derive macro for C-style primitive enums.
homepage
repositoryhttps://github.com/Protryon/cenum
max_upload_size
id212957
size44,540
Max Bruce (Protryon)

documentation

README

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);
}
Commit count: 13

cargo fmt