Crate e_num

source ·
Expand description

E-Num(ber)

Serialize enums into numbers.

WARNING

This library works with variant fields (e.g. Variant1(u64)) by bitshifting the number representation of the contained value over enough so that the tagging can fit on the right of the number. If you’re dealing with very large numbers in the fields or have a ton of variants, data on the left side of the value will likely be lost.

Basic Usage

#[macro_use]
extern crate e_num;

use e_num::ENum;

#[derive(ENum)]
enum A {
  B,
  C(u64),
}

fn main() {
  let b: usize = A::B.to_num();
  println!("b as a number: {:#b}", b);
  let b = A::from_num(b);
  assert!(match b {
    A::B => true,
    _ => false,
  });
  let c = A::C(85).to_num();
  println!("c as a number: {:#b}", c);
  let c = A::from_num(c);
  assert!(match c {
    A::C(inner) => {
      assert_eq!(inner, 85);
      true
    }
    _ => false,
  });
}

start_at and constant variants

#[macro_use]
extern crate e_num;

use e_num::ENum;

#[derive(ENum)]
// where the non-constant variants will start counting from
#[e_num(start_at = 9)]
enum A {
  // pulls the specified variant out from the rest of them
  // and matches it against that number. constant variants
  // can't have a field.
  #[e_num(constant = 2)]
  B,
  C,
  D,
  E,
}

fn main() {
  assert_eq!(A::B.to_num(), 2);
  assert_eq!(A::C.to_num(), 9);
  assert_eq!(A::D.to_num(), 10);
  assert_eq!(A::E.to_num(), 11);
}

Traits