#memory-layout #control #explicit #options #variant #pattern #niches

controlled-option

Custom Option type with explicit control over niches and memory layout

5 releases (3 breaking)

0.4.1 Aug 24, 2021
0.4.0 Aug 24, 2021
0.3.0 Aug 24, 2021
0.2.0 Aug 24, 2021
0.1.0 Aug 23, 2021

#892 in Text processing

Download history 130/week @ 2024-04-09 120/week @ 2024-04-16 152/week @ 2024-04-23 210/week @ 2024-04-30 283/week @ 2024-05-07 221/week @ 2024-05-14 184/week @ 2024-05-21 351/week @ 2024-05-28 245/week @ 2024-06-04 358/week @ 2024-06-11 196/week @ 2024-06-18 193/week @ 2024-06-25 196/week @ 2024-07-02 227/week @ 2024-07-09 157/week @ 2024-07-16 154/week @ 2024-07-23

799 downloads per month
Used in 7 crates (2 directly)

MIT/Apache

19KB
457 lines

controlled-option

This crate provides a custom implementation of the standard Option, where you have explicit control over how the None variant is laid out in memory.


lib.rs:

This crate provides a replacement for the standard Option type where you have full control over how the None and Some variants are represented in memory.

Normally, you don't have to think about this. The standard Option is a perfectly normal enum, and the compiler takes care of determining the most efficient in-memory representation. In particular, the compiler knows that certain types have niches — in-memory bit patterns that do not represent valid values of the type. If a type has a niche, then the compiler can use that bit pattern to represent the None variant. This works automatically for most of the types you might care about: in particular, for references and the various NonZero types in std::num.

However, sometimes a type has multiple possible niches, and you need control over which one the compiler chooses to use. Or, you might have defined a type such that the compiler cannot see that it has a niche available to use. In this case, you can use the Niche and ControlledOption types from this crate to take full control over how the None and Some variants are laid out in memory.

Dependencies

~1.5MB
~35K SLoC