title: How do I return a reference to a concrete type that was added to a vector of trait objects?
body: <p>In this code, I take a vector, create a struct instance, and add it to the vector boxed:</p>
  
  <pre><code>trait T {}
  
  struct X {}
  impl T for X {}
  
  fn add_inst(vec: &amp;mut Vec&lt;Box&lt;T&gt;&gt;) -&gt; &amp;X {
      let x = X {};
      vec.push(Box::new(x));
      // Ugly, unsafe hack I made
      unsafe { std::mem::transmute(&amp;**vec.last().unwrap()) }
  }
  </code></pre>
  
  <p>Obviously, it uses <code>mem::transmute</code>, which makes me feel it's not the right way to do this. Is this ugly hack the only way to do it?</p>
  
  <p>Additionally, while this compiles in Rust 1.32, it fails in Rust 1.34:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
    --&gt; src/lib.rs:10:14
     |
  10 |     unsafe { std::mem::transmute(&amp;**vec.last().unwrap()) }
     |              ^^^^^^^^^^^^^^^^^^^
     |
     = note: source type: `&amp;dyn T` (128 bits)
     = note: target type: `&amp;X` (64 bits)
  </code></pre>