<p dir="auto">I think the following snippet should be possible (but it fails currently). Since <code class="notranslate">&amp;'static MyTrait</code> can't be 0, if the first half of the layout is 0, then the enum must be the <code class="notranslate">Boxed</code> variant.</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="use std::mem;

pub trait MyTrait {
    fn zomg(&amp;self);
}

enum Foo {
    Inline(&amp;'static MyTrait, [usize; 2]),
    Boxed(Box&lt;MyTrait&gt;),
}

pub fn main() {
    assert_eq!(2 * mem::size_of::&lt;&amp;'static MyTrait&gt;(), mem::size_of::&lt;Foo&gt;());
}"><pre class="notranslate"><span class="pl-k">use</span> std<span class="pl-kos">::</span>mem<span class="pl-kos">;</span>

<span class="pl-k">pub</span> <span class="pl-k">trait</span> <span class="pl-smi">MyTrait</span> <span class="pl-kos">{</span>
    <span class="pl-k">fn</span> <span class="pl-en">zomg</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">enum</span> <span class="pl-smi">Foo</span> <span class="pl-kos">{</span>
    <span class="pl-v">Inline</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-c1">'</span><span class="pl-ent">static</span> <span class="pl-smi">MyTrait</span><span class="pl-kos">,</span> <span class="pl-kos">[</span><span class="pl-smi">usize</span><span class="pl-kos">;</span> <span class="pl-c1">2</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-v">Boxed</span><span class="pl-kos">(</span><span class="pl-smi">Box</span><span class="pl-kos">&lt;</span><span class="pl-smi">MyTrait</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span>

<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-en">assert_eq</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-c1">2</span> * mem::size_of::&lt;&amp;<span class="pl-c1">'</span><span class="pl-k">static</span> <span class="pl-v">MyTrait</span>&gt;<span class="pl-kos">(</span><span class="pl-kos">)</span>, mem::size_of::&lt;<span class="pl-v">Foo</span>&gt;<span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">The current implementation only handles enums of roughly the form:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="enum E {
    A(ALeft..., Niche, ARight...),
    B,
    C
}"><pre class="notranslate"><span class="pl-k">enum</span> <span class="pl-smi">E</span> <span class="pl-kos">{</span>
    <span class="pl-v">A</span><span class="pl-kos">(</span><span class="pl-smi">ALeft</span>...<span class="pl-kos">,</span> <span class="pl-smi">Niche</span><span class="pl-kos">,</span> <span class="pl-smi">ARight</span>...<span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-v">B</span><span class="pl-kos">,</span>
    <span class="pl-v">C</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">This can be seen as a special-case, where B and C occupy 0 bytes, of:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="enum E {
    A(ALeft..., Niche, ARight...),
    B(B...),
    C(C...)
}"><pre class="notranslate"><span class="pl-k">enum</span> <span class="pl-smi">E</span> <span class="pl-kos">{</span>
    <span class="pl-v">A</span><span class="pl-kos">(</span><span class="pl-smi">ALeft</span>...<span class="pl-kos">,</span> <span class="pl-smi">Niche</span><span class="pl-kos">,</span> <span class="pl-smi">ARight</span>...<span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-v">B</span><span class="pl-kos">(</span><span class="pl-smi">B</span>...<span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-v">C</span><span class="pl-kos">(</span><span class="pl-smi">C</span>...<span class="pl-kos">)</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">As long as <code class="notranslate">B</code> and <code class="notranslate">C</code> can fit before or after <code class="notranslate">A</code>'s <code class="notranslate">Niche</code>, we can still apply the optimization.<br>
Also see <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="98328835" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rfcs/issues/1230" data-hovercard-type="issue" data-hovercard-url="/rust-lang/rfcs/issues/1230/hovercard?comment_id=345456535&amp;comment_type=issue_comment" href="https://github.com/rust-lang/rfcs/issues/1230#issuecomment-345456535">rust-lang/rfcs#1230 (comment)</a> for the initial description.</p>