from_int   Latest Version

Motivation

This crate provides an easy way to convert a plain integer into an enum type, something that rust can currently do natively in the opposite direction.

Usage

Usage of from_int is extremely simple. Just add it as a dependency to your crate, then:

```rust extern crate from_int; // contains the trait

[macrouse] extern crate fromint_derive; // contains the macro

use from_int::FromInt;

[derive(FromInt, Debug, PartialEq)]

enum TestEnum { VariantOne = 1, VariantTwo = 2, VariantThree = 528, VariantX = 999 }

asserteq!(TestEnum::VariantOne, TestEnum::fromint(1).unwrap()); asserteq!(TestEnum::VariantTwo, TestEnum::fromint(2).unwrap()); asserteq!(TestEnum::VariantThree, TestEnum::fromint(528).unwrap()); asserteq!(TestEnum::VariantX, TestEnum::fromint(999).unwrap());

// This would panic: asserteq!(None, TestEnum::fromint(123)); ```