from_int 0.1.2

A package to easily add a from_int method to enums with #[derive(FromInt)]
Documentation
  • Coverage
  • 33.33%
    1 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 4.38 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.06 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • nickdonnelly/from_int
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nickdonnelly

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