Skip to content

A simple, lightweight rust crate that allows you to easily derive an enum value from an underlying integer.

Notifications You must be signed in to change notification settings

nickdonnelly/from_int

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

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));

About

A simple, lightweight rust crate that allows you to easily derive an enum value from an underlying integer.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages