byte-enum

Crates.iobyte-enum
lib.rsbyte-enum
version0.1.2
sourcesrc
created_at2022-05-24 06:05:00.539543
updated_at2022-05-24 06:11:48.144974
descriptionA derive macro for converting #[repr(u8)] enums to bytes and back.
homepage
repositoryhttps://github.com/recatek/byte-enum
max_upload_size
id592514
size7,648
(recatek)

documentation

README

byte-enum

A ByteEnum derive macro and trait to implement Into<u8> and TryFrom<u8> for a #[repr(u8)] enum. Simple as.

The enum must be #[repr(u8)], fieldless, and may not have explicit discriminants.


use byte_enum::ByteEnum;

#[derive(ByteEnum)]
#[repr(u8)]
enum SomeEnum {
    VariantA,
    VariantB,
    VariantC,
}

let b: u8 = SomeEnum::VariantB.into();
assert_eq!(b, 1);
 
let c = SomeEnum::try_from(2_u8);
assert_eq!(c, Ok(SomeEnum::VariantC));
 
let d = SomeEnum::try_from(4_u8);
assert!(d.is_err());

License

byte-enum may be used under your choice of the Apache 2 or MIT license.

Commit count: 19

cargo fmt