3 releases

0.1.2 Oct 14, 2019
0.1.1 Jun 24, 2019
0.1.0 May 7, 2019

#1271 in Procedural macros

Download history 4468/week @ 2025-08-01 4418/week @ 2025-08-08 4881/week @ 2025-08-15 4037/week @ 2025-08-22 3323/week @ 2025-08-29 4617/week @ 2025-09-05 3617/week @ 2025-09-12 4119/week @ 2025-09-19 3869/week @ 2025-09-26 3751/week @ 2025-10-03 4009/week @ 2025-10-10 4329/week @ 2025-10-17 3861/week @ 2025-10-24 4195/week @ 2025-10-31 4833/week @ 2025-11-07 3802/week @ 2025-11-14

17,414 downloads per month
Used in 23 crates (14 directly)

MIT license

48KB
900 lines

enum-utils

crates.io docs.rs Build Status

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());

Dependencies

~4–5MB
~110K SLoC