# Allow impossible values of slice length to be used as enum discriminant

**URL:** <https://internals.rust-lang.org/t/allow-impossible-values-of-slice-length-to-be-used-as-enum-discriminant/23933>\
**Category:** compiler\
**Created:** [January 17, 2026, 12:52pm UTC](https://internals.rust-lang.org/t/allow-impossible-values-of-slice-length-to-be-used-as-enum-discriminant/23933 "2026-01-17T12:52:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruttie](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ruttie/32/13137_2.png) [@Ruttie](https://internals.rust-lang.org/u/Ruttie)\
**Post date:** [January 17, 2026, 12:52pm UTC](https://internals.rust-lang.org/t/allow-impossible-values-of-slice-length-to-be-used-as-enum-discriminant/23933/1 "2026-01-17T12:52:39Z")

</div>

For non-ZST slices, the length of the slice is required to be \<= isize::MAX.  
Currently, enums containing slices like:

```rs
enum Example<'a> {
    Variant1(&'a [u8]),
    Variant2,
    Variant3
}

```

Have a size of 24 bytes.  
Given u8 is not zero-sized, and the length should thus be \<= isize::MAX, the length could double as the variant ID, allowing the size to be shrunk to 16 bytes.

An example real-world usecase would be a Value enum for (de)serializers:

```rs
pub enum Value<'de> {
    Int(u64),
    Float(f64),
    String(&'de str),
    List(u32),
    Map(u32),
    Null
}

```

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [January 17, 2026, 1:22pm UTC](https://internals.rust-lang.org/t/allow-impossible-values-of-slice-length-to-be-used-as-enum-discriminant/23933/2 "2026-01-17T13:22:56Z")

</div>

> [@Ruttie](#):
>
> Given u8 is not zero-sized, and the length should thus be \<= isize::MAX, the length could double as the variant ID, allowing the size to be shrunk to 16 bytes.

`Example` still has to be 24 bytes even when the length has a niche for `> isize::MAX`. This because it must be possible to take a reference to the `&[u8]` field of both variants, which requires that they both have the real length stored and thus can't use the upper bit as discriminant to distinguish between the two.

---

<div class="post-metadata">

**Author:** ![Ruttie](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ruttie/32/13137_2.png) [@Ruttie](https://internals.rust-lang.org/u/Ruttie)\
**Post date:** [January 17, 2026, 1:27pm UTC](https://internals.rust-lang.org/t/allow-impossible-values-of-slice-length-to-be-used-as-enum-discriminant/23933/3 "2026-01-17T13:27:57Z")

</div>

True, I've updated the example, thanks for pointing it out!

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 17, 2026, 10:52pm UTC](https://internals.rust-lang.org/t/allow-impossible-values-of-slice-length-to-be-used-as-enum-discriminant/23933/4 "2026-01-17T22:52:10Z")

</div>

This is absolutely something that everyone wants, but it's challenging because of cycles and performance -- the size of the niche depends on the size of the element type, but you could have things where the element type depends on the self type.

It'll probably need adding a "safe underestimate of the size and alignment" query, but then the problem is adding that extra query without making the compiler perf worse.

That said, the most important different is ZST-ness vs non-ZST-ness, as `&[()]` has no niches in the length. So maybe there's a simplified version that could get most of the _niche_ usefulness by just doing that -- `isize::MAX` length niches is probably more than can realistically be used anyway, even if there could be more than that sometimes.

cc also [cleanup quasi-layout alignment computations · Issue #147107 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/147107)
