#enums #macro-derive #numeric-type #repr #create #byte #conversion

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

#2230 in Procedural macros

Download history 104/week @ 2023-11-20 138/week @ 2023-11-27 72/week @ 2023-12-04 148/week @ 2023-12-11 114/week @ 2023-12-18 177/week @ 2023-12-25 82/week @ 2024-01-01 145/week @ 2024-01-08 202/week @ 2024-01-15 234/week @ 2024-01-22 203/week @ 2024-01-29 91/week @ 2024-02-05 128/week @ 2024-02-12 302/week @ 2024-02-19 277/week @ 2024-02-26 439/week @ 2024-03-04

1,146 downloads per month
Used in 3 crates (2 directly)

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
~33K SLoC