#enums #integer #converting #type #repr #macro #int-enum

macro no-std int-enum-impl

Implementation detail of the int-enum crate

5 releases (breaking)

0.5.0 Oct 6, 2022
0.4.0 Feb 23, 2020
0.3.0 Dec 20, 2019
0.2.0 Dec 18, 2019
0.1.0 Oct 21, 2019

#30 in #repr

Download history 1163/week @ 2023-11-30 1475/week @ 2023-12-07 1737/week @ 2023-12-14 1034/week @ 2023-12-21 896/week @ 2023-12-28 1920/week @ 2024-01-04 2063/week @ 2024-01-11 1774/week @ 2024-01-18 1160/week @ 2024-01-25 1503/week @ 2024-02-01 1469/week @ 2024-02-08 1533/week @ 2024-02-15 1411/week @ 2024-02-22 1686/week @ 2024-02-29 2024/week @ 2024-03-07 1291/week @ 2024-03-14

6,642 downloads per month

MIT/Apache

17KB
391 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.0"

Compiler support: requires rustc 1.64+

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

~3.5MB
~73K SLoC