#enum #primitive #macro #conversion #enums #enum-primitive

enum_primitive

Macro to generate num::FromPrimitive instances for enum that works in Rust 1.0

4 releases

Uses old Rust 2015

0.1.1 Jan 9, 2017
0.1.0 Aug 13, 2015
0.0.2 Apr 8, 2015
0.0.1 Apr 7, 2015

#1011 in Rust patterns

Download history 11627/week @ 2022-04-17 12345/week @ 2022-04-24 10198/week @ 2022-05-01 9597/week @ 2022-05-08 10659/week @ 2022-05-15 11987/week @ 2022-05-22 15299/week @ 2022-05-29 14605/week @ 2022-06-05 12114/week @ 2022-06-12 12864/week @ 2022-06-19 13454/week @ 2022-06-26 9208/week @ 2022-07-03 8628/week @ 2022-07-10 9556/week @ 2022-07-17 11677/week @ 2022-07-24 14280/week @ 2022-07-31

44,793 downloads per month
Used in 457 crates (79 directly)

MIT license

9KB
126 lines

enum_primitive Build Status

This crate exports a macro enum_from_primitive! that wraps an enum declaration and automatically adds an implementation of num::FromPrimitive (reexported here), to allow conversion from primitive integers to the enum. It therefore provides an alternative to the built-in #[derive(FromPrimitive)], which requires the unstable std::num::FromPrimitive and is disabled in Rust 1.0.

Documentation

https://andersk.github.io/enum_primitive-rs/enum_primitive/

Usage

Add the following to your Cargo.toml file:

[dependencies]
enum_primitive = "*"

Import the crate using #[macro_use] extern crate enum_primitive, and wrap your enum declaration inside the enum_from_primitive! macro.

Example

#[macro_use] extern crate enum_primitive;
extern crate num;
use num::FromPrimitive;

enum_from_primitive! {
#[derive(Debug, PartialEq)]
enum FooBar {
    Foo = 17,
    Bar = 42,
    Baz,
}
}

fn main() {
    assert_eq!(FooBar::from_i32(17), Some