Expand description
§TryFromInt
A convenient derive macro for converting an integer to a C-like enum.
This crate provides a derive procedural macro named TryFromInt.
This macro will automatically implement
TryFrom trait
for enums that meet the following requirements:
- The enum must have a primitive repr, i.e., the enum should have attribute like
#[repr(u8)],#[repr(u32)], etc. The type parameter ofTryFromwill be the repr. - The enum must consist solely of unit variants, which is called units only enum. Each field should have an explicit discriminant.
§Quick Start
To use this crate, first add this crate to your Cargo.toml.
[dependencies]
int-to-c-enum = "0.1.0"Below is a simple example. We derive macro TryFromInt for an enum Color.
use int_to_c_enum::TryFromInt;
#[repr(u8)]
#[derive(TryFromInt, Debug, Eq, PartialEq)]
pub enum Color {
Red = 1,
Yellow = 2,
Blue = 3,
}
// Then, we can use method `try_from` for `Color`.
let color = Color::try_from(1).unwrap();
assert!(color == Color::Red);§Macro Expansion
The TryFromInt macro will automatically implement trait TryFrom<u8> for Color.
After macro expansion, the generated code looks like as follows:
ⓘ
impl TryFrom<u8> for Color {
type Error = TryFromIntError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
1 => Ok(Color::Red),
2 => Ok(Color::Yellow),
3 => Ok(Color::Blue),
_ => Err(TryFromIntError::InvalidValue),
}
}
}§License
This project is licensed under MPL-2.0.
Enums§
- TryFrom
IntError - Error type for TryFromInt derive macro