title: How do I evaluate expressions in Rust's macro system?
body: <p>I'm trying to learn the Rust macro system by writing a simple macro that generates a struct based on some unsigned integer type (<code>u8</code>, <code>u16</code>, <code>u32</code>, <code>u64</code>). I want something like this:</p>
  
  <pre><code>bitmessage! {
      struct Header(u16);
      version: 8, 5; // the first number is the length, second is value
      data: 8, 5;
  }
  </code></pre>
  
  <p>To be more specific, I'm looking for some way to store certain information in an unsigned integer type with various offsets. One use case is to read some bytes and construct some kind of "message":</p>
  
  <pre class="lang-none prettyprint-override"><code>[ 15 14 13 12 11 10 09 08 | 07 06 05 04 03 02 01 01 ]
  </code></pre>
  
  <p>The higher part of the message contains some data/information, the lower part a versioning field. (This is just a toy example).</p>
  
  <p>This is my effort so far, but the inner repeating expansion does not compile: </p>
  
  <pre><code>macro_rules! bitmessage {
  (struct $name:ident($n:ty); 
      $($field_name:ident: $length:expr, $value:expr;)*)  =&gt; {
           struct $name ($n);
           $($name.1 = $name.1 | $value &lt;&lt; $length)*
      };
  }
  </code></pre>
  
  <p>One solution could be to store the relevant bytes in a struct, implementing it directly (or with a trait) to get the appropriate fields, but this would involve too much bit-shifting logic (no problem with that, but there must be a more convenient way).</p>
  
  <p>I am aware of <a href="https://crates.io/crates/bitflags" rel="nofollow noreferrer">bitflags</a> and <a href="https://crates.io/crates/bitfield" rel="nofollow noreferrer">bitfield</a>. Neither of them matches my use case.</p>