enum_primitive-0.1.1/.gitignore01006640001750000175000000000022125106655560014765 0ustar0000000000000000target Cargo.lock enum_primitive-0.1.1/.travis.yml01006640001750000175000000000067130345521500015102 0ustar0000000000000000language: rust rust: - 1.0.0 - stable - nightly enum_primitive-0.1.1/Cargo.toml01006440001750000175000000002001130345517500014712 0ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "enum_primitive" version = "0.1.1" authors = ["Anders Kaseorg "] description = "Macro to generate num::FromPrimitive instances for enum that works in Rust 1.0" homepage = "https://github.com/andersk/enum_primitive-rs" documentation = "https://andersk.github.io/enum_primitive-rs/enum_primitive/" readme = "README.md" license = "MIT" repository = "https://github.com/andersk/enum_primitive-rs.git" [dependencies.num-traits] version = "0.1.32" default-features = false enum_primitive-0.1.1/Cargo.toml.orig01006440001750000175000000000766130345517500015671 0ustar0000000000000000[package] name = "enum_primitive" version = "0.1.1" license = "MIT" description = "Macro to generate num::FromPrimitive instances for enum that works in Rust 1.0" authors = ["Anders Kaseorg "] documentation = "https://andersk.github.io/enum_primitive-rs/enum_primitive/" repository = "https://github.com/andersk/enum_primitive-rs.git" homepage = "https://github.com/andersk/enum_primitive-rs" readme = "README.md" [dependencies.num-traits] version = "0.1.32" default-features = false enum_primitive-0.1.1/LICENSE01006640001750000175000000002074125107045460014004 0ustar0000000000000000Copyright (c) 2015 Anders Kaseorg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. enum_primitive-0.1.1/README.md01006640001750000175000000002435125112065110014245 0ustar0000000000000000# enum_primitive [![Build Status](https://travis-ci.org/andersk/enum_primitive-rs.svg?branch=master)](https://travis-ci.org/andersk/enum_primitive-rs) 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 ```rust #[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(FooBar::Foo)); assert_eq!(FooBar::from_i32(42), Some(FooBar::Bar)); assert_eq!(FooBar::from_i32(43), Some(FooBar::Baz)); assert_eq!(FooBar::from_i32(91), None); } ``` enum_primitive-0.1.1/src/lib.rs01006440001750000175000000015452130345510020014671 0ustar0000000000000000// Copyright (c) 2015 Anders Kaseorg // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // “Software”), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! 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. //! //! # Example //! //! ``` //! #[macro_use] extern crate enum_primitive; //! extern crate num_traits; //! use num_traits::FromPrimitive; //! //! enum_from_primitive! { //! #[derive(Debug, PartialEq)] //! enum FooBar { //! Foo = 17, //! Bar = 42, //! Baz, //! } //! } //! //! fn main() { //! assert_eq!(FooBar::from_i32(17), Some(FooBar::Foo)); //! assert_eq!(FooBar::from_i32(42), Some(FooBar::Bar)); //! assert_eq!(FooBar::from_i32(43), Some(FooBar::Baz)); //! assert_eq!(FooBar::from_i32(91), None); //! } //! ``` extern crate num_traits; pub use std::option::Option; pub use num_traits::FromPrimitive; /// Helper macro for internal use by `enum_from_primitive!`. #[macro_export] macro_rules! enum_from_primitive_impl_ty { ($meth:ident, $ty:ty, $name:ident, $( $variant:ident )*) => { #[allow(non_upper_case_globals, unused)] fn $meth(n: $ty) -> $crate::Option { $( if n == $name::$variant as $ty { $crate::Option::Some($name::$variant) } else )* { $crate::Option::None } } }; } /// Helper macro for internal use by `enum_from_primitive!`. #[macro_export] #[macro_use(enum_from_primitive_impl_ty)] macro_rules! enum_from_primitive_impl { ($name:ident, $( $variant:ident )*) => { impl $crate::FromPrimitive for $name { enum_from_primitive_impl_ty! { from_i64, i64, $name, $( $variant )* } enum_from_primitive_impl_ty! { from_u64, u64, $name, $( $variant )* } } }; } /// Wrap this macro around an `enum` declaration to get an /// automatically generated implementation of `num::FromPrimitive`. #[macro_export] #[macro_use(enum_from_primitive_impl)] macro_rules! enum_from_primitive { ( $( #[$enum_attr:meta] )* enum $name:ident { $( $( #[$variant_attr:meta] )* $variant:ident ),+ $( = $discriminator:expr, $( $( #[$variant_two_attr:meta] )* $variant_two:ident ),+ )* } ) => { $( #[$enum_attr] )* enum $name { $( $( #[$variant_attr] )* $variant ),+ $( = $discriminator, $( $( #[$variant_two_attr] )* $variant_two ),+ )* } enum_from_primitive_impl! { $name, $( $variant )+ $( $( $variant_two )+ )* } }; ( $( #[$enum_attr:meta] )* enum $name:ident { $( $( $( #[$variant_attr:meta] )* $variant:ident ),+ = $discriminator:expr ),* } ) => { $( #[$enum_attr] )* enum $name { $( $( $( #[$variant_attr] )* $variant ),+ = $discriminator ),* } enum_from_primitive_impl! { $name, $( $( $variant )+ )* } }; ( $( #[$enum_attr:meta] )* enum $name:ident { $( $( #[$variant_attr:meta] )* $variant:ident ),+ $( = $discriminator:expr, $( $( #[$variant_two_attr:meta] )* $variant_two:ident ),+ )*, } ) => { $( #[$enum_attr] )* enum $name { $( $( #[$variant_attr] )* $variant ),+ $( = $discriminator, $( $( #[$variant_two_attr] )* $variant_two ),+ )*, } enum_from_primitive_impl! { $name, $( $variant )+ $( $( $variant_two )+ )* } }; ( $( #[$enum_attr:meta] )* enum $name:ident { $( $( $( #[$variant_attr:meta] )* $variant:ident ),+ = $discriminator:expr ),+, } ) => { $( #[$enum_attr] )* enum $name { $( $( $( #[$variant_attr] )* $variant ),+ = $discriminator ),+, } enum_from_primitive_impl! { $name, $( $( $variant )+ )+ } }; ( $( #[$enum_attr:meta] )* pub enum $name:ident { $( $( #[$variant_attr:meta] )* $variant:ident ),+ $( = $discriminator:expr, $( $( #[$variant_two_attr:meta] )* $variant_two:ident ),+ )* } ) => { $( #[$enum_attr] )* pub enum $name { $( $( #[$variant_attr] )* $variant ),+ $( = $discriminator, $( $( #[$variant_two_attr] )* $variant_two ),+ )* } enum_from_primitive_impl! { $name, $( $variant )+ $( $( $variant_two )+ )* } }; ( $( #[$enum_attr:meta] )* pub enum $name:ident { $( $( $( #[$variant_attr:meta] )* $variant:ident ),+ = $discriminator:expr ),* } ) => { $( #[$enum_attr] )* pub enum $name { $( $( $( #[$variant_attr] )* $variant ),+ = $discriminator ),* } enum_from_primitive_impl! { $name, $( $( $variant )+ )* } }; ( $( #[$enum_attr:meta] )* pub enum $name:ident { $( $( #[$variant_attr:meta] )* $variant:ident ),+ $( = $discriminator:expr, $( $( #[$variant_two_attr:meta] )* $variant_two:ident ),+ )*, } ) => { $( #[$enum_attr] )* pub enum $name { $( $( #[$variant_attr] )* $variant ),+ $( = $discriminator, $( $( #[$variant_two_attr] )* $variant_two ),+ )*, } enum_from_primitive_impl! { $name, $( $variant )+ $( $( $variant_two )+ )* } }; ( $( #[$enum_attr:meta] )* pub enum $name:ident { $( $( $( #[$variant_attr:meta] )* $variant:ident ),+ = $discriminator:expr ),+, } ) => { $( #[$enum_attr] )* pub enum $name { $( $( $( #[$variant_attr] )* $variant ),+ = $discriminator ),+, } enum_from_primitive_impl! { $name, $( $( $variant )+ )+ } }; } enum_primitive-0.1.1/tests/image.rs01006640001750000175000000003624125112024070015561 0ustar0000000000000000#[macro_use] extern crate enum_primitive; mod gif { enum_from_primitive! { /// Known block types enum Block { Image = 0x2C, Extension = 0x21, Trailer = 0x3B } } enum_from_primitive! { /// Known GIF extensions enum Extension { Text = 0x01, Control = 0xF9, Comment = 0xFE, Application = 0xFF } } enum_from_primitive! { /// Method to dispose the image enum DisposalMethod { Undefined = 0, None = 1, Previous = 2, Background = 3 } } } mod png { enum_from_primitive! { #[derive(Clone, Copy, Debug, PartialEq)] enum InterlaceMethod { None = 0, Adam7 = 1 } } enum_from_primitive! { #[derive(Debug)] pub enum FilterType { NoFilter = 0, Sub = 1, Up = 2, Avg = 3, Paeth = 4 } } } mod tiff { enum_from_primitive! { #[derive(Clone, Copy, Debug, PartialEq)] enum PhotometricInterpretation { WhiteIsZero = 0, BlackIsZero = 1, RGB = 2, RGBPalette = 3, TransparencyMask = 4, CMYK = 5, YCbCr = 6, CIELab = 8, } } enum_from_primitive! { #[derive(Clone, Copy, Debug)] enum CompressionMethod { None = 1, Huffman = 2, Fax3 = 3, Fax4 = 4, LZW = 5, JPEG = 6, PackBits = 32773 } } enum_from_primitive! { #[derive(Clone, Copy, Debug)] enum PlanarConfiguration { Chunky = 1, Planar = 2 } } enum_from_primitive! { #[derive(Clone, Copy, Debug)] enum Predictor { None = 1, Horizontal = 2 } } enum_from_primitive! { #[derive(Clone, Copy, Debug)] pub enum Type { BYTE = 1, ASCII = 2, SHORT = 3, LONG = 4, RATIONAL = 5, } } } enum_primitive-0.1.1/tests/omitted-discriminants.rs01006640001750000175000000007033125112057730021020 0ustar0000000000000000#[macro_use] extern crate enum_primitive; enum_from_primitive! { enum E { } } enum_from_primitive! { enum E0 { V0 } } enum_from_primitive! { enum E0C { V0, } } enum_from_primitive! { enum E1 { V0 = 0 } } enum_from_primitive! { enum E1C { V0 = 0, } } enum_from_primitive! { enum E00 { V0, V1 } } enum_from_primitive! { enum E00C { V0, V1, } } enum_from_primitive! { enum E01 { V0, V1 = 1 } } enum_from_primitive! { enum E01C { V0, V1 = 1, } } enum_from_primitive! { enum E10 { V0 = 0, V1 } } enum_from_primitive! { enum E10C { V0 = 0, V1, } } enum_from_primitive! { enum E11 { V0 = 0, V1 = 1 } } enum_from_primitive! { enum E11C { V0 = 0, V1 = 1, } } enum_from_primitive! { enum E000 { V0, V1, V2 } } enum_from_primitive! { enum E000C { V0, V1, V2, } } enum_from_primitive! { enum E001 { V0, V1, V2 = 2 } } enum_from_primitive! { enum E001C { V0, V1, V2 = 2, } } enum_from_primitive! { enum E010 { V0, V1 = 1, V2 } } enum_from_primitive! { enum E010C { V0, V1 = 1, V2, } } enum_from_primitive! { enum E011 { V0, V1 = 1, V2 = 2 } } enum_from_primitive! { enum E011C { V0, V1 = 1, V2 = 2, } } enum_from_primitive! { enum E100 { V0 = 0, V1, V2 } } enum_from_primitive! { enum E100C { V0 = 0, V1, V2, } } enum_from_primitive! { enum E101 { V0 = 0, V1, V2 = 2 } } enum_from_primitive! { enum E101C { V0 = 0, V1, V2 = 2, } } enum_from_primitive! { enum E110 { V0 = 0, V1 = 1, V2 } } enum_from_primitive! { enum E110C { V0 = 0, V1 = 1, V2, } } enum_from_primitive! { enum E111 { V0 = 0, V1 = 1, V2 = 2 } } enum_from_primitive! { enum E111C { V0 = 0, V1 = 1, V2 = 2, } } enum_from_primitive! { enum E0000 { V0, V1, V2, V3 } } enum_from_primitive! { enum E0000C { V0, V1, V2, V3, } } enum_from_primitive! { enum E0001 { V0, V1, V2, V3 = 3 } } enum_from_primitive! { enum E0001C { V0, V1, V2, V3 = 3, } } enum_from_primitive! { enum E0010 { V0, V1, V2 = 2, V3 } } enum_from_primitive! { enum E0010C { V0, V1, V2 = 2, V3, } } enum_from_primitive! { enum E0011 { V0, V1, V2 = 2, V3 = 3 } } enum_from_primitive! { enum E0011C { V0, V1, V2 = 2, V3 = 3, } } enum_from_primitive! { enum E0100 { V0, V1 = 1, V2, V3 } } enum_from_primitive! { enum E0100C { V0, V1 = 1, V2, V3, } } enum_from_primitive! { enum E0101 { V0, V1 = 1, V2, V3 = 3 } } enum_from_primitive! { enum E0101C { V0, V1 = 1, V2, V3 = 3, } } enum_from_primitive! { enum E0110 { V0, V1 = 1, V2 = 2, V3 } } enum_from_primitive! { enum E0110C { V0, V1 = 1, V2 = 2, V3, } } enum_from_primitive! { enum E0111 { V0, V1 = 1, V2 = 2, V3 = 3 } } enum_from_primitive! { enum E0111C { V0, V1 = 1, V2 = 2, V3 = 3, } } enum_from_primitive! { enum E1000 { V0 = 0, V1, V2, V3 } } enum_from_primitive! { enum E1000C { V0 = 0, V1, V2, V3, } } enum_from_primitive! { enum E1001 { V0 = 0, V1, V2, V3 = 3 } } enum_from_primitive! { enum E1001C { V0 = 0, V1, V2, V3 = 3, } } enum_from_primitive! { enum E1010 { V0 = 0, V1, V2 = 2, V3 } } enum_from_primitive! { enum E1010C { V0 = 0, V1, V2 = 2, V3, } } enum_from_primitive! { enum E1011 { V0 = 0, V1, V2 = 2, V3 = 3 } } enum_from_primitive! { enum E1011C { V0 = 0, V1, V2 = 2, V3 = 3, } } enum_from_primitive! { enum E1100 { V0 = 0, V1 = 1, V2, V3 } } enum_from_primitive! { enum E1100C { V0 = 0, V1 = 1, V2, V3, } } enum_from_primitive! { enum E1101 { V0 = 0, V1 = 1, V2, V3 = 3 } } enum_from_primitive! { enum E1101C { V0 = 0, V1 = 1, V2, V3 = 3, } } enum_from_primitive! { enum E1110 { V0 = 0, V1 = 1, V2 = 2, V3 } } enum_from_primitive! { enum E1110C { V0 = 0, V1 = 1, V2 = 2, V3, } } enum_from_primitive! { enum E1111 { V0 = 0, V1 = 1, V2 = 2, V3 = 3 } } enum_from_primitive! { enum E1111C { V0 = 0, V1 = 1, V2 = 2, V3 = 3, } } enum_primitive-0.1.1/tests/tests.rs01006440001750000175000000012434130345607750015656 0ustar0000000000000000#[macro_use] extern crate enum_primitive as ep; enum_from_primitive! { enum Unused { A = 17, B = 42 } } enum_from_primitive! { #[derive(Debug, PartialEq)] enum Empty { } } #[test] fn empty() { use ep::FromPrimitive; assert_eq!(Empty::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] enum One { A = 17 } } #[test] fn one() { use ep::FromPrimitive; assert_eq!(One::from_isize(17), Some(One::A)); assert_eq!(One::from_isize(91), None); assert_eq!(One::from_i8(17), Some(One::A)); assert_eq!(One::from_i8(91), None); assert_eq!(One::from_i16(17), Some(One::A)); assert_eq!(One::from_i16(91), None); assert_eq!(One::from_i32(17), Some(One::A)); assert_eq!(One::from_i32(91), None); assert_eq!(One::from_i64(17), Some(One::A)); assert_eq!(One::from_i64(91), None); assert_eq!(One::from_usize(17), Some(One::A)); assert_eq!(One::from_usize(91), None); assert_eq!(One::from_u8(17), Some(One::A)); assert_eq!(One::from_u8(91), None); assert_eq!(One::from_u16(17), Some(One::A)); assert_eq!(One::from_u16(91), None); assert_eq!(One::from_u32(17), Some(One::A)); assert_eq!(One::from_u32(91), None); assert_eq!(One::from_u64(17), Some(One::A)); assert_eq!(One::from_u64(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] enum OneComma { A = 17, } } #[test] fn one_comma() { use ep::FromPrimitive; assert_eq!(OneComma::from_i32(17), Some(OneComma::A)); assert_eq!(OneComma::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] enum Two { A = 17, B = 42 } } #[test] fn two() { use ep::FromPrimitive; assert_eq!(PubTwo::from_i32(17), Some(PubTwo::A)); assert_eq!(PubTwo::from_i32(42), Some(PubTwo::B)); assert_eq!(PubTwo::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] enum TwoComma { A = 17, B = 42, } } #[test] fn two_comma() { use ep::FromPrimitive; assert_eq!(TwoComma::from_i32(17), Some(TwoComma::A)); assert_eq!(TwoComma::from_i32(42), Some(TwoComma::B)); assert_eq!(TwoComma::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] pub enum PubEmpty { } } #[test] fn pub_empty() { use ep::FromPrimitive; assert_eq!(PubEmpty::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] pub enum PubOne { A = 17 } } #[test] fn pub_one() { use ep::FromPrimitive; assert_eq!(PubOne::from_i32(17), Some(PubOne::A)); assert_eq!(PubOne::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] pub enum PubOneComma { A = 17, } } #[test] fn pub_one_comma() { use ep::FromPrimitive; assert_eq!(PubOneComma::from_i32(17), Some(PubOneComma::A)); assert_eq!(PubOneComma::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] pub enum PubTwo { A = 17, B = 42 } } #[test] fn pub_two() { use ep::FromPrimitive; assert_eq!(PubTwo::from_i32(17), Some(PubTwo::A)); assert_eq!(PubTwo::from_i32(42), Some(PubTwo::B)); assert_eq!(PubTwo::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] pub enum PubTwoComma { A = 17, B = 42, } } #[test] fn pub_two_comma() { use ep::FromPrimitive; assert_eq!(PubTwoComma::from_i32(17), Some(PubTwoComma::A)); assert_eq!(PubTwoComma::from_i32(42), Some(PubTwoComma::B)); assert_eq!(PubTwoComma::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] enum Negative { A = -17 } } #[test] fn negative() { use ep::FromPrimitive; assert_eq!(Negative::from_isize(-17), Some(Negative::A)); assert_eq!(Negative::from_isize(-91), None); assert_eq!(Negative::from_i8(-17), Some(Negative::A)); assert_eq!(Negative::from_i8(-91), None); assert_eq!(Negative::from_i16(-17), Some(Negative::A)); assert_eq!(Negative::from_i16(-91), None); assert_eq!(Negative::from_i32(-17), Some(Negative::A)); assert_eq!(Negative::from_i32(-91), None); assert_eq!(Negative::from_i64(-17), Some(Negative::A)); assert_eq!(Negative::from_i64(-91), None); assert_eq!(Negative::from_usize(!16), Some(Negative::A)); assert_eq!(Negative::from_usize(!90), None); assert_eq!(Negative::from_u8(!16), None); assert_eq!(Negative::from_u8(!90), None); assert_eq!(Negative::from_u16(!16), None); assert_eq!(Negative::from_u16(!90), None); assert_eq!(Negative::from_u32(!16), None); assert_eq!(Negative::from_u32(!90), None); assert_eq!(Negative::from_u64(!16), Some(Negative::A)); assert_eq!(Negative::from_u64(!90), None); } #[test] fn in_local_mod() { mod local_mod { enum_from_primitive! { #[derive(Debug, PartialEq)] pub enum InLocalMod { A = 17, B = 42, } } } use ep::FromPrimitive; assert_eq!(local_mod::InLocalMod::from_i32(17), Some(local_mod::InLocalMod::A)); assert_eq!(local_mod::InLocalMod::from_i32(42), Some(local_mod::InLocalMod::B)); assert_eq!(local_mod::InLocalMod::from_i32(91), None); } enum_from_primitive! { #[derive(Debug, PartialEq)] #[doc = "Documented"] pub enum Documented { A = 17 } } #[test] fn documented() { use ep::FromPrimitive; assert_eq!(Documented::from_i32(17), Some(Documented::A)); assert_eq!(Documented::from_i32(91), None); }