2 releases
Uses old Rust 2015
0.1.2 | Mar 28, 2018 |
---|---|
0.1.0 | Mar 27, 2018 |
#52 in #int
Used in from_int
4KB
64 lines
from_int
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:
extern crate from_int; // contains the trait
#[macro_use] extern crate from_int_derive; // contains the macro
use from_int::FromInt;
#[derive(FromInt, Debug, PartialEq)]
enum TestEnum {
VariantOne = 1,
VariantTwo = 2,
VariantThree = 528,
VariantX = 999
}
assert_eq!(TestEnum::VariantOne, TestEnum::from_int(1).unwrap());
assert_eq!(TestEnum::VariantTwo, TestEnum::from_int(2).unwrap());
assert_eq!(TestEnum::VariantThree, TestEnum::from_int(528).unwrap());
assert_eq!(TestEnum::VariantX, TestEnum::from_int(999).unwrap());
// This would panic:
assert_eq!(None, TestEnum::from_int(123));
Dependencies
~2MB
~47K SLoC