3 releases

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

#1614 in Procedural macros

Download history 4249/week @ 2025-09-21 3726/week @ 2025-09-28 3839/week @ 2025-10-05 3784/week @ 2025-10-12 4377/week @ 2025-10-19 4052/week @ 2025-10-26 4668/week @ 2025-11-02 4794/week @ 2025-11-09 5127/week @ 2025-11-16 4201/week @ 2025-11-23 3682/week @ 2025-11-30 3142/week @ 2025-12-07 3106/week @ 2025-12-14 3004/week @ 2025-12-21 1773/week @ 2025-12-28 3012/week @ 2026-01-04

11,052 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

~6MB
~131K SLoC