title: Box like container FixedSizeBox<128, dyn SomeTrait> which can be constructed from type implementing SomeTrait
body: <p>I would like to implement a <code>FixedSizeBox</code> which behaves like the <code>Box</code> but owns the memory in which the object is stored. I introduced an additional parameter <code>CAPACITY</code> which defines the size of the memory which the <code>FixedSizeBox</code> will own.</p>
  <p>The problem I encounter is that I am unable to use <code>dyn SomeTrait</code> as <code>FixedSizeBox</code> type and call <code>new</code> with a specific type.</p>
  <p>Let's assume I have a trait <code>SomeTrait</code> and a struct <code>A</code> which implements that trait.</p>
  <pre class="lang-rust prettyprint-override"><code>trait SomeTrait {}
  struct A(u8);
  impl SomeTrait for A {}
  </code></pre>
  <p>When I would like to store any object which implements <code>SomeTrait</code> in a <code>Box</code> I can write</p>
  <pre class="lang-rust prettyprint-override"><code>let mut a: Box&lt;dyn SomeTrait&gt;;
  a = Box::new(A { 0: 0 });
  </code></pre>
  <p>For the <code>FixedSizeBox</code> I would like to have the same functionality and implemented it like:</p>
  <pre class="lang-rust prettyprint-override"><code>pub struct FixedSizeBox&lt;const CAPACITY: usize, T: ?Sized&gt; {
      _marker: PhantomData&lt;T&gt;,
  }
  
  impl&lt;const CAPACITY: usize, T&gt; FixedSizeBox&lt;CAPACITY, T&gt; {
      pub fn new(_: T) -&gt; FixedSizeBox&lt;CAPACITY, T&gt; {
          FixedSizeBox::&lt;CAPACITY, T&gt; {
              _marker: PhantomData,
          }
      }
  }
  </code></pre>
  <p>When I use it in the same fashion as the <code>Box</code> the compiler fails:</p>
  <pre class="lang-rust prettyprint-override"><code>let mut b: StackBox&lt;128, dyn SomeTrait&gt;;
  // assigning a specific type leads to
  // mismatched types
  // expected struct `StackBox&lt;dyn SomeTrait, 128_usize&gt;`
  //    found struct `StackBox&lt;A, {_: usize}&gt;` (rustc E0308)
  b = StackBox::new(A { 0: 0 });
  </code></pre>
  <p>I looked into the <code>Box</code> and <code>Rc</code> implementations but was unable to figure out how rust actually realized this. Is there some trait missing?</p>
  <p>As a side constraint, I would like to use stable rust. I could figure out that the nightly feature <code>CoerceUnsized</code></p>
  <pre class="lang-rust prettyprint-override"><code>impl&lt;T, U&gt; std::ops::CoerceUnsized&lt;StackBox&lt;U&gt;&gt; for FixedSizeBox&lt;T&gt;
  where
      T: std::marker::Unsize&lt;U&gt; + ?Sized,
      U: ?Sized,
  {
  }
  </code></pre>
  <p>solves the issue but I would like to understand how this is realized in stable rust for <code>Box</code> so that I can apply it to the <code>FixedSizeBox</code>.</p>