# Pre-RFC enum from integer

**URL:** <https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348>\
**Category:** language design\
**Created:** [December 6, 2017, 6:41pm UTC](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348 "2017-12-06T18:41:13Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [March 16, 2018, 5:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348/21 "2018-03-16T17:26:43Z")

</div>

Sure, there’s [`enum_primitive`](https://crates.io/crates/enum_primitive) using macros 1.0, and [`enum-primitive-derive`](https://crates.io/crates/enum-primitive-derive) and [`num-derive`](https://crates.io/crates/num-derive) using custom derive – all targeting `FromPrimitive`. I’m sure `TryFrom` could be derived the same way, although automatically writing this for just the `repr` type is harder, since we don’t have that type info AFAIK.

---

<div class="post-metadata">

**Author:** ![vlisivka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vlisivka/32/4887_2.png) [@vlisivka](https://internals.rust-lang.org/u/vlisivka)\
**Post date:** [November 9, 2018, 7:29pm UTC](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348/22 "2018-11-09T19:29:35Z")

</div>

Proposed solution:

```
#[repr(C)]
enum Foo {
  Bar = 0,
  Baz = 1,
  Other(i32),
}

```

Where `Other(i32)` is **virtual** field only allowed for `repr(C)` enums, which represents other possible variants for enum, like in C.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [November 10, 2018, 12:05am UTC](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348/23 "2018-11-10T00:05:58Z")

</div>

Here’s an idea:

```rust
unsafe trait CEnum {
  type Underlying: Copy;
}
// mod std::intrinsics
fn from_discriminant<E: CEnum>(val: E::Underlying) -> Option<E> { /* filled in by compiler */ }

```

`CEnum` is automatically implemented for `#[repr(uN)]` or `#[repr(C)]` enums, in “the way one expects”. The intrinsic function does what it says on the tin: eats a number and returns the corresponding variant, if it exists. The actual lookup table for whether an enum is in range is only created when `from_discriminant` is monomorphized.

---

<div class="post-metadata">

**Author:** ![illicitonion](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/illicitonion/32/3886_2.png) [@illicitonion](https://internals.rust-lang.org/u/illicitonion)\
**Post date:** [November 12, 2018, 9:31am UTC](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348/24 "2018-11-12T09:31:49Z")

</div>

I put together [https://crates.io/crates/num\_enum](https://crates.io/crates/num_enum) which allows you to derive `TryFrom` just for the repr type (by deriving `TryFromPrimitive`). It also allows you to derive a `TryInto` implementation which works on stable (by deriving `CustomTryInto`) because `TryFrom` isn’t stable yet, but it’s a little ugly/hacky. It was actually refreshingly simple to put together (and my first proc\_macro)!

It currently doesn’t allow deriving `#[repr(C)]` enums because the target size isn’t well-specified.

I’m very open to feedback/improvements, if anyone’s interested.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348/25 "2019-03-25T08:29:20Z")

</div>

Closed on March 25, 2019, 8:29am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/pre-rfc-enum-from-integer/6348.md?page=1)
