enum_like 0.2.1

A trait to treat any type as an enum. If a type has a reasonably small number of variants, for example a `struct A(bool, bool)` which has 4 variants, this trait provides a 1-to-1 mapping from type value to a discriminant value. It also provides an iterator over all the possible values. This crate is mainly meant to be used along with [enum_like_derive](https://crates.io/crates/enum_like_derive) and [enum_vec](https://crates.io/crates/enum_vec) .
Documentation
  • Coverage
  • 100%
    14 out of 14 items documented2 out of 12 items with examples
  • Size
  • Source code size: 26.59 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.78 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Badel2/enum_vec
    6 0 2
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Badel2

This crate provides the EnumLike trait, which defines a mapping from a given type to usize.

This is similar to std::mem::discriminant, however it has a few differences. First of all, all the values are consecutive starting from zero. This means that if an enum has 10 variants, the discriminant will always be lower than 10. If a field has an explicit discriminant, that value is ignored: in enum { A = 100 }, A will have the value of 0. And most importantly, this trait allows to create an instance of the type from the usize, because the enum data, if present, is also encoded in the discriminant (if possible). For example:

enum ABC { A, B, C }
enum DEF { D, E, F }
enum AD { A(ABC), D(DEF) }

The AD enum has 2 variants, but since each of these variants is an enum with 3 variants, the AD::values().count() will return 6 instead of 2.