#enums #macro #byte #macro-derive

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

#107 in #enum

Download history 76/week @ 2024-03-09 82/week @ 2024-03-16 135/week @ 2024-03-23 297/week @ 2024-03-30 58/week @ 2024-04-06 53/week @ 2024-04-13 47/week @ 2024-04-20 23/week @ 2024-04-27 74/week @ 2024-05-04 96/week @ 2024-05-11 82/week @ 2024-05-18 40/week @ 2024-05-25 52/week @ 2024-06-01 71/week @ 2024-06-08 38/week @ 2024-06-15 95/week @ 2024-06-22

264 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
~35K SLoC