int-enum

Crates.ioint-enum
lib.rsint-enum
version1.1.2
sourcesrc
created_at2019-10-21 14:40:40.241209
updated_at2024-05-15 09:44:37.594268
descriptionA derive macro for conversion between integer and enum types.
homepage
repositoryhttps://github.com/Juici/int-enum-rs
max_upload_size
id174472
size29,926
James (Juici)

documentation

https://docs.rs/int-enum

README

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.

Commit count: 53

cargo fmt