#enums #macro #macro-derive #byte

macro enum2repr

EnumRepr is a rust derive macro that creates conversion methods to map between a value and an enum. Numeric types supported by #[repr(T)] are supported by enum2repr.

4 releases

0.1.14 Apr 10, 2023
0.1.13 Mar 27, 2023

#504 in #macro-derive

Download history 459/week @ 2024-03-13 555/week @ 2024-03-20 836/week @ 2024-03-27 406/week @ 2024-04-03 747/week @ 2024-04-10 1230/week @ 2024-04-17 769/week @ 2024-04-24 737/week @ 2024-05-01 1045/week @ 2024-05-08 877/week @ 2024-05-15 764/week @ 2024-05-22 814/week @ 2024-05-29 682/week @ 2024-06-05 747/week @ 2024-06-12 710/week @ 2024-06-19 802/week @ 2024-06-26

3,164 downloads per month
Used in auto_uds

MIT license

6KB
70 lines

enum2repr

github crates.io docs.rs

enum2repr is a rust derive macro that creates conversion methods to map between a value and an enum. Numeric types supported by #[repr(T)] are supported by enum2repr.

Usage

Add this to your Cargo.toml:

enum2repr = "0.1.14"

Example:

use enum2repr::EnumRepr;

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

#[test]
fn convert_variants() {
    assert_eq!(Ok(Color::Red), Color::try_from(0x04));
    assert_eq!(Ok(Color::Green), Color::try_from(0x15));
    assert_eq!(Ok(Color::Blue), Color::try_from(0x34));
}

#[test]
fn convert_variants_back() {
    assert_eq!(u16::from(Color::Red), 0x04);
    assert_eq!(u16::from(Color::Green), 0x15);
    assert_eq!(u16::from(Color::Blue), 0x34);
}

Dependencies

~1.5MB
~35K SLoC