title: Compile-time generic type size check
body: <p>I'm attempting to write Rust bindings for a C collection library (Judy Arrays [1]) which only provides itself room to store a pointer-width value. My company has a fair amount of existing code which uses this space to directly store non-pointer values such as pointer-width integers and small structs. I'd like my Rust bindings to allow type-safe access to such collections using generics, but am having trouble getting the pointer-stashing semantics working correctly.</p>
  
  <p>I have a basic interface working using <code>std::mem::transmute_copy()</code> to store the value, but that function explicitly does nothing to ensure the source and destination types are the same size.  I'm able to verify that collection type parameter is of a compatible size at run-time via an assertion, but I'd really like the check to somehow be at compile-time.</p>
  
  <p>Example code:</p>
  
  <pre><code>pub struct Example&lt;T&gt; {
      v: usize,
      t: PhantomData&lt;T&gt;,
  }
  
  impl&lt;T&gt; Example&lt;T&gt; {
      pub fn new() -&gt; Example&lt;T&gt; {
          assert!(mem::size_of::&lt;usize&gt;() == mem::size_of::&lt;T&gt;());
          Example { v: 0, t: PhantomData }
      }
  
      pub fn insert(&amp;mut self, val: T) {
          unsafe {
              self.v = mem::transmute_copy(&amp;val);
              mem::forget(val);
          }
      }
  }
  </code></pre>
  
  <p>Is there a better way to do this, or is this run-time check the best Rust 1.0 supports?</p>
  
  <p>(<a href="https://stackoverflow.com/questions/30328366/pointer-stashing-generics-via-memtransmute">Related question</a>, explaining why I'm not using <code>mem::transmute()</code>.)</p>
  
  <p>[1] I'm aware of the existing rust-judy project, but it doesn't support the pointer-stashing I want, and I'm writing these new bindings largely as a learning exercise anyway.</p>