Crates.io | enum-utils |
lib.rs | enum-utils |
version | 0.1.2 |
source | src |
created_at | 2019-05-07 21:27:46.199143 |
updated_at | 2019-10-14 16:55:12.226091 |
description | A set of useful proc macros for enums |
homepage | |
repository | https://github.com/ecstatic-morse/enum-utils |
max_upload_size | |
id | 132689 |
size | 37,468 |
A set of procedural macros for deriving useful functionality on enums.
See the API docs for more information.
FromStr
An efficient, configurable FromStr
implementation for C-like enums.
#[derive(Debug, PartialEq, enum_utils::FromStr)]
enum Test {
Alpha,
Beta,
}
assert_eq!("Alpha".parse(), Ok(Test::Alpha));
assert_eq!("Beta".parse(), Ok(Test::Beta));
IterVariants
A static method returning an iterator over the variants of an enum.
#[derive(Debug, PartialEq, Eq, enum_utils::IterVariants)]
#[repr(u8)]
pub enum Direction {
North = 1,
East,
South,
West,
}
use Direction::*;
assert_eq!(Direction::iter().collect::<Vec<_>>(), vec![North, East, South, West]);
TryFromRepr
and ReprFrom
Conversion to and from the discriminant of a C-like enum.
use std::convert::TryInto;
#[derive(Debug, Clone, Copy, PartialEq, Eq, enum_utils::ReprFrom, enum_utils::TryFromRepr)]
#[repr(u8)]
pub enum Direction {
North = 1,
East,
South,
West
}
use Direction::*;
assert_eq!(1u8, North.into());
assert_eq!(4u8, West.into());
assert_eq!(North, 1u8.try_into().unwrap());
assert_eq!(West, 4u8.try_into().unwrap());