bytenum

Crates.iobytenum
lib.rsbytenum
version0.1.9
sourcesrc
created_at2022-12-09 05:08:32.738728
updated_at2022-12-12 15:53:08.311393
descriptionBytenum is a rust derive macro that creates a try_from implementation for an enum with only unit variants. All types supported by #[repr(T)] are supported by bytenum.
homepagehttps://github.com/matthewjberger/bytenum
repositoryhttps://github.com/matthewjberger/bytenum
max_upload_size
id732995
size6,607
Matthew J. Berger (matthewjberger)

documentation

README

Bytenum

Bytenum is a rust derive macro that creates a try_from<T> implementation for an enum with only unit variants. All types supported by #[repr(T)] are supported by bytenum.

Usage

Add this to your Cargo.toml:

bytenum = "0.1.9"

Example:

use bytenum::Bytenum;

#[derive(Bytenum, Debug, PartialEq, Copy, Clone)]
#[repr(u16)]
enum Color {
    Red = 0x04,
    Green = 0x15,
    Blue = 0x34,
}

fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
    assert_eq!(Color::Red, Color::try_from(0x04)?);
    assert_eq!(Color::Green, Color::try_from(0x15)?);
    assert_eq!(Color::Blue, Color::try_from(0x34)?);
    Ok(())
}
Commit count: 18

cargo fmt