title: How to create a DST type?
body: <p>DST (Dynamically Sized Types) are a thing in Rust now. I have <em>used</em> them successfully, with a flexible last member which is known to the compiler (such as <code>[u8]</code>).</p>
  
  <p>What I am looking to do, however, is to create a custom DST. Say, for example:</p>
  
  <pre><code>struct Known&lt;S&gt; {
      dropit: fn (&amp;mut S) -&gt; (),
      data: S,
  }
  
  struct Unknown {
      dropit: fn (&amp;mut ()) -&gt; (),
      data: (),
  }
  </code></pre>
  
  <p>With an expected usage being <code>Box&lt;Known&lt;S&gt;&gt;</code> => <code>Box&lt;Unknown&gt;</code> => <code>Box&lt;Known&lt;S&gt;&gt;</code>, where the middleware need not know about concrete types.</p>
  
  <p><em>Note: yes, I know about <code>Any</code>, and no I am not interested in using it.</em></p>
  
  <p>I am open to suggestions in the layout of both <code>Known</code> and <code>Unknown</code>, however:</p>
  
  <ol>
  <li><code>size_of::&lt;Box&lt;Known&gt;&gt;() = size_of::&lt;Box&lt;Unknown&gt;&gt;() = size_of::&lt;Box&lt;u32&gt;&gt;()</code>; that is it should be a thin pointer.</li>
  <li>dropping <code>Box&lt;Unknown&gt;</code> drops its content</li>
  <li>cloning <code>Box&lt;Unknown&gt;</code> (assuming a clonable <code>S</code>), clones its content</li>
  <li>ideally, <code>fn dup(u: &amp;Unknown) -&gt; Box&lt;Unknown&gt; { box u.clone() }</code> works</li>
  </ol>
  
  <p>I have particular difficulties with (3) and (4), I could solve (3) with manually allocating memory (not using <code>box</code>, but directly calling <code>malloc</code>) but I would prefer providing an idiomatic experience to the user.</p>
  
  <p>I could not find any documentation on how to inform <code>box</code> of the right size to allocate.</p>