| Crates.io | int-to-c-enum |
| lib.rs | int-to-c-enum |
| version | 0.1.1 |
| created_at | 2024-07-01 08:21:05.870457+00 |
| updated_at | 2025-06-27 09:13:36.715475+00 |
| description | TryFromInt - A convenient derive macro for converting an integer to an enum |
| homepage | |
| repository | https://github.com/asterinas/int-to-c-enum |
| max_upload_size | |
| id | 1288547 |
| size | 6,477 |
To use this crate, first add this crate to your Cargo.toml.
[dependencies]
int-to-c-enum = "0.1.0"
You can use this macro for a C-like enum.
use int_to_c_enum::TryFromInt;
#[repr(u8)]
#[derive(TryFromInt, Debug)]
pub enum Color {
Red = 1,
Blue = 2,
Green = 3,
}
Then, you can use try_from function for this enum.
fn main() {
let color = Color::try_from(1).unwrap();
println!("color = {color:?}"); // color = Red;
}
This crate provides a derive procedural macro named TryFromInt. This macro will automatically implement TryFrom trait for enums that meet the following requirements:
QuickStart example, the macro will implement TryFrom<u8> for Color.