#enums #macro-derive #integer #conversion #proc-macro #int #type

macro int-enum

A derive macro for conversion between integer and enum types

10 releases (4 stable)

new 1.1.2 May 15, 2024
1.1.1 Mar 18, 2024
1.0.1 Feb 17, 2024
0.5.0 Oct 6, 2022
0.1.1 Nov 25, 2019

#86 in Procedural macros

Download history 1209/week @ 2024-01-25 1623/week @ 2024-02-01 1494/week @ 2024-02-08 1700/week @ 2024-02-15 1471/week @ 2024-02-22 1792/week @ 2024-02-29 2123/week @ 2024-03-07 1882/week @ 2024-03-14 2175/week @ 2024-03-21 2181/week @ 2024-03-28 1528/week @ 2024-04-04 1794/week @ 2024-04-11 1822/week @ 2024-04-18 2898/week @ 2024-04-25 2490/week @ 2024-05-02 1514/week @ 2024-05-09

8,989 downloads per month
Used in 44 crates (19 directly)

MIT/Apache

12KB
192 lines

derive(IntEnum)

github crates.io docs.rs build status

This crate provides a convenient derive macro for the core library's From and TryFrom traits for converting between integer and enum types.

[dependencies]
int-enum = "1.1"

Compiler support: requires rustc 1.70+

Example

use int_enum::IntEnum;

#[repr(u8)]
#[derive(Debug, PartialEq, IntEnum)]
pub enum Ascii {
   UpperA = b'A',
   UpperB = b'B',
}

assert_eq!(u8::from(Ascii::UpperA), b'A');
assert_eq!(u8::from(Ascii::UpperB), b'B');

assert_eq!(Ascii::try_from(b'A'), Ok(Ascii::UpperA));
assert_eq!(Ascii::try_from(b'B'), Ok(Ascii::UpperB));
assert_eq!(Ascii::try_from(b'C'), Err(b'C'));

License

This project is licensed under either of Apache License, Version 2.0 or MIT License at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.4–0.8MB
~19K SLoC