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

macro int-enum

A derive macro for conversion between integer and enum types

10 releases (4 stable)

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

#87 in Procedural macros

Download history 1833/week @ 2024-07-21 1914/week @ 2024-07-28 2317/week @ 2024-08-04 2351/week @ 2024-08-11 2269/week @ 2024-08-18 1934/week @ 2024-08-25 2012/week @ 2024-09-01 2400/week @ 2024-09-08 1678/week @ 2024-09-15 2364/week @ 2024-09-22 2427/week @ 2024-09-29 1851/week @ 2024-10-06 1951/week @ 2024-10-13 2569/week @ 2024-10-20 1915/week @ 2024-10-27 1995/week @ 2024-11-03

8,564 downloads per month
Used in 51 crates (22 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

~285–750KB
~18K SLoC