title: Rust bincode enum binary serialisation
body: <p>I'm fairly new to Rust and am using bincode in my project.  The encoding of enum variants is an issue that I've been trying to deal with as I am interfacing with an existing server.  The old project was written in C and the compilier option of &quot;fshort-enums&quot; was defined.</p>
  <p>In the old C project I would define an enum something like:</p>
  <pre class="lang-c prettyprint-override"><code>enum test_enum {
      Start,
      Init,
      Complete,
  }
  </code></pre>
  <p>and then when this data was put into the packet it would take up 1 byte like this:</p>
  <pre class="lang-none prettyprint-override"><code>0x00,
  0x01,
  0x02
  </code></pre>
  <p>If I defined the enum as:</p>
  <pre class="lang-c prettyprint-override"><code>enum test_enum {
      Start,
      Init,
      Complete = 65535,
  }
  </code></pre>
  <p>then when this data was put into the packet every variant would take up 2 bytes like this:</p>
  <pre class="lang-none prettyprint-override"><code>0x00 0x00,
  0x01 0x00,
  0x02 0x00
  </code></pre>
  <p>Now I've found a way to configure <code>bincode</code> enum encoding to change from the default 4 bytes to be the appropriate size for the value like this:</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Serialize, Deserialize)]
  enum TestEnum {
      Start,
      Init,
      Complete = 65535,
  }
  
  let x = TestEnum::Complete;
  if let Ok(bytes) = bincode::options().with_varint_encoding().serialize(&amp;x) {
      println!(&quot;{:?}&quot;, bytes); // [2]
  }
  </code></pre>
  <p>Question1: How do I force <code>bincode</code> to make all variants take up 2 bytes?</p>
  <p>Question2: How do I force <code>bincode</code> to apply the enum variant value?  (in this example <code>TestEnum::Complete</code> has a value of 2 instead of 65535).</p>