| Crates.io | byte-enum-derive |
| lib.rs | byte-enum-derive |
| version | 0.1.2 |
| created_at | 2022-05-24 05:56:10.856081+00 |
| updated_at | 2022-05-24 06:11:36.510999+00 |
| description | Procedural macro implementation for byte-enum. |
| homepage | |
| repository | https://github.com/recatek/byte-enum |
| max_upload_size | |
| id | 592507 |
| size | 6,556 |
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());
byte-enum may be used under your choice of the Apache 2 or MIT license.