title: Is there a way to use a boolean as an underlying type for an enum?
body: <p>I decided to implement a protocol which uses a couple of flags, so I started to define <code>enum</code>s for the flags. However, when I want to define a flag that has two values which can be <code>true</code> or <code>false</code> I get an error message:</p>
  
  <pre><code>// The protocol definition says that the flag 
  // can have two values true or false, so I could just use 
  // plain bool, but I want another name for true and false.
  enum Flag {
      ONE = true,
      TWO = false,
  }
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
   --&gt; src/lib.rs:5:11
    |
  5 |     ONE = true,
    |           ^^^^ expected isize, found bool
  
  error[E0308]: mismatched types
   --&gt; src/lib.rs:6:11
    |
  6 |     TWO = false,
    |           ^^^^^ expected isize, found bool
  </code></pre>
  
  <p>The reason I want to use an enum instead of two constants is that the flag is not an bool. It is flag with representation value true or false, but I don't want to mix normal <code>bool</code>s and flag. If I used <code>bool</code> constants, I could pass the flag value to every function that takes a <code>bool</code> as argument, or use them in expressions as <code>bool</code>, e.g.</p>
  
  <pre><code>if ONE {
  }
  
  fn some_function_with_a_flag(b: bool);
  // I don't want this!
  some_function_with_a_flag(ONE);
  </code></pre>
  
  <p>Using an enum instead of bool constants also prevents some more errors when using the flag as a struct member. There are more flags defined in the same way, so when I just use plain <code>bool</code>s and constants I will have a struct like</p>
  
  <pre><code>struct Header {
      flag1: bool,
      flag2: bool,
      flag3: bool,
  }
  </code></pre>
  
  <p>The compiler will accept code where the flag values are switched:</p>
  
  <pre><code>h = Header { flag3: ONE, flag1: TWO, flag2: ONE };
  </code></pre>
  
  <p>That is not possible when each flag is its own type (alias for <code>bool</code>).</p>
  
  <p>The point of defining the enum with values <code>true</code> and <code>false</code> is just that the protocol defines it that way. In my code, I will probably only use the boolean value of the flags when data is packed to be serialized (it is part of the data header).</p>
  
  <p>Ok, the compiler always assumes that the underlying type is <code>isize</code>. It could derive it from the values, but let's define it</p>
  
  <pre><code>#[repr(bool)]
  enum E1 {
      ONE = true,
      TWO = false,
  }
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>error[E0552]: unrecognized representation hint
   --&gt; src/lib.rs:1:8
    |
  1 | #[repr(bool)]
    |        ^^^^
  </code></pre>
  
  <p>It looks like I have to use <code>u8</code> as the underlying type and then always cast the value into <code>bool</code></p>
  
  <pre><code>#[repr(u8)]
  enum E2 {
      ONE = 1,
      TWO = 0,
  }
  
  let x = E2::ONE as bool;
  </code></pre>
  
  <p>This compiles, but seems overly complicated. Is there a better way to define an <code>enum</code> with <code>bool</code> values? Is there an idiom for a type alias of <code>bool</code> where I can specify the value? I could just do</p>
  
  <pre><code>enum Flag {
      TWO, 
      ONE,
  }
  </code></pre>
  
  <p>but now I again have to cast the value to <code>bool</code> all the time, and the order of definition looks unnatural.</p>
  
  <p>Since the bool value will only be used when reading/writing the header I will just put the conversion into the corresponding functions and keep the rest of the program free from implementation details.</p>