enum2repr

Crates.ioenum2repr
lib.rsenum2repr
version0.1.14
sourcesrc
created_at2023-03-26 22:25:54.092056
updated_at2023-04-10 02:43:39.021699
descriptionEnumRepr 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.
homepagehttps://github.com/matthewjberger/enum2repr
repositoryhttps://github.com/matthewjberger/enum2repr
max_upload_size
id821539
size8,787
Matthew J. Berger (matthewjberger)

documentation

README

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);
}
Commit count: 10

cargo fmt