Skip to content

A derive macro for converting #[repr(u8)] enums to bytes and back.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

recatek/byte_enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.0 or MIT license.

About

A derive macro for converting #[repr(u8)] enums to bytes and back.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages