#enums #byte

macro 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.

10 releases

0.1.9 Dec 12, 2022
0.1.8 Dec 12, 2022

#392 in #byte

Download history 10/week @ 2025-07-28 20/week @ 2025-08-04 38/week @ 2025-08-11 58/week @ 2025-08-18 32/week @ 2025-08-25 48/week @ 2025-09-01 26/week @ 2025-09-08 65/week @ 2025-09-15 76/week @ 2025-09-22 78/week @ 2025-09-29 115/week @ 2025-10-06 75/week @ 2025-10-13 34/week @ 2025-10-20 36/week @ 2025-10-27 10/week @ 2025-11-03 15/week @ 2025-11-10

97 downloads per month

MIT license

6KB
64 lines

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(())
}

Dependencies

~1.5MB
~39K SLoC