# Towards even smaller structs

**URL:** <https://internals.rust-lang.org/t/towards-even-smaller-structs/14686>\
**Category:** language design\
**Created:** [May 12, 2021, 4:05pm UTC](https://internals.rust-lang.org/t/towards-even-smaller-structs/14686 "2021-05-12T16:05:12Z")\
**Posts on this page:** 1\
**Showing post:** 39

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [May 15, 2021, 12:40am UTC](https://internals.rust-lang.org/t/towards-even-smaller-structs/14686/39 "2021-05-15T00:40:58Z")

</div>

> [@canndrew](#):
>
> One possible suggestion: It might be nice if we had a `uint<N>` type for `N: usize` . These would have next-power-of-two size and alignment normally but would be treated specially by the compiler when used as a `#[flatten]` field. This probably has a bunch of complications though and might be stretching the weirdness budget 😕.

It's wouldn't be that special. All you need is to say that the out-of-range values are a niche of the type. For example, `uint<6>` would be represented as `u8`, but values 64 through 255 are a niche. Then at least from a theoretical perspective, the compiler being allowed to optimize this into a bitfield is a natural result of the representation being unspecified (combined with the inability to take references). Even from a concrete implementation perspective, the compiler could look at niches when deciding whether to optimize into a bitfield, instead of literally special-casing `uint<N>`.

Still, the use of a bit width would be confusing, as users might expect that to always result in a bitfield. But that's easily solved. Instead of defining the type in terms of bits, define a more generic 'integer in this range' type . For example, `uint<0, 42>` would represent an integer from 0 to 42. It would have niches from 43 to 255, and when combined with `#[flat]` would typically be represented in 6 bits. This kind of type would be useful for high-level tasks, completely independently from its use for bitfields.

---

_[View the full topic](https://internals.rust-lang.org/t/towards-even-smaller-structs/14686)._
