#enum #open #integer #newtype

no-std bin+lib open-enum

An attribute for generating "open" fieldless enums, those that accept any integer value, by using a newtype struct and associated constants

6 releases (breaking)

0.5.0 Feb 15, 2024
0.4.1 Nov 18, 2023
0.4.0 Sep 2, 2023
0.3.0 Sep 12, 2022
0.1.0 Sep 2, 2022

#287 in Rust patterns

Download history 1178/week @ 2023-11-09 1083/week @ 2023-11-16 740/week @ 2023-11-23 824/week @ 2023-11-30 1049/week @ 2023-12-07 1055/week @ 2023-12-14 451/week @ 2023-12-21 450/week @ 2023-12-28 1176/week @ 2024-01-04 945/week @ 2024-01-11 1379/week @ 2024-01-18 1428/week @ 2024-01-25 2143/week @ 2024-02-01 1734/week @ 2024-02-08 2135/week @ 2024-02-15 2253/week @ 2024-02-22

8,542 downloads per month
Used in 2 crates


106 lines


Rust enums are closed, meaning that the integer value distinguishing an enum, its discriminant, must be one of the variants listed. If the integer value isn't one of those discriminants, it is considered immediate undefined behavior. This is true for enums with and without fields. This can make working with enums troublesome in high performance code that can't afford premature runtime checks. It can also introduce Undefined Behavior at unexpected times if the author is unfamiliar with the rules of writing unsafe Rust.

In constrast, C++ scoped enumerations are open, meaning that the enum is a strongly-typed integer that could hold any value, though with a scoped set of well-known values. open-enum lets you have this in Rust. It turns this enum declaration:

enum Color {

into a tuple struct with associated constants:

#[derive(PartialEq, Eq)]  // In order to work in `match`.
struct Color(pub u8);  // Automatic integer type, can be specified.

impl Color {
    pub const Red: Self = Color(0);
    pub const Green: Self = Color(1);
    pub const Blue: Self = Color(2);
    pub const Orange: Self = Color(3);
    pub const Black: Self = Color(4);


See CONTRIBUTING.md for details.


Apache 2.0; see LICENSE for details.


This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.


~19K SLoC