| Crates.io | discriminant-rs |
| lib.rs | discriminant-rs |
| version | 0.1.2 |
| created_at | 2025-03-05 13:06:52.947385+00 |
| updated_at | 2025-03-05 13:19:18.20477+00 |
| description | Convert enum to integer type |
| homepage | |
| repository | https://github.com/MMitsuha/discriminant |
| max_upload_size | |
| id | 1579010 |
| size | 8,506 |
Convert a enum (with or without field) with #[repr(...)] to corresponding type. Compatible with #![no_std].
Tested on rust-2021 and rust-2024
use discriminant::Discriminant;
#[derive(Discriminant)]
#[repr(i16)]
enum Test {
One = 1,
Two = 2,
Four = 4,
}
fn test() {
assert_eq!(Test::One.discriminant(), 1);
assert_eq!(Test::Two.discriminant(), 2);
assert_eq!(Test::Four.discriminant(), 4);
}
I had been seen such an approach on StackOverflow long time ago, but I can not find the original post, to make it convenient for other developers dealing with lower level data struct, I made this crate. Thank for the author on StackOverflow.