Crates.io | numeric-enum-macro |
lib.rs | numeric-enum-macro |
version | 0.2.0 |
source | src |
created_at | 2020-02-25 14:05:39.75802 |
updated_at | 2020-03-02 13:15:56.023334 |
description | A declarative macro for type-safe enum-to-numbers conversion |
homepage | |
repository | https://github.com/mexus/numeric-enum-macro |
max_upload_size | |
id | 212409 |
size | 17,433 |
A declarative macro for type-safe enum-to-numbers conversion. no-std
supported!
use numeric_enum_macro::numeric_enum;
numeric_enum! {
#[repr(i64)] // repr must go first.
/// Some docs.
///
/// Multiline docs works too.
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] // all the attributes are forwarded!
pub enum Lol {
// All the constants must have explicit values assigned!
Kek = 14,
Wow = 87,
}
}
// Conversion to raw number:
assert_eq!(14i64, Lol::Kek.into());
// Conversion from raw number:
assert_eq!(Ok(Lol::Wow), Lol::try_from(87));
// Unknown number:
assert_eq!(Err(88), Lol::try_from(88));
License: MIT/Apache-2.0