#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

#68 in #enum

Download history 49/week @ 2024-11-16 9/week @ 2024-11-23 19/week @ 2024-11-30 20/week @ 2024-12-07 1/week @ 2024-12-14 11/week @ 2024-12-28 38/week @ 2025-01-04 35/week @ 2025-01-11 68/week @ 2025-01-18 4/week @ 2025-01-25 120/week @ 2025-02-01 85/week @ 2025-02-08 36/week @ 2025-02-15 43/week @ 2025-02-22 81/week @ 2025-03-01

268 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
~38K SLoC