Crate controlled_option[−][src]
Expand description
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.
Structs
An Option
type where you have control over the in-memory representation of the None
and
Some
variants. See the module-level documentation for more information.
Traits
A type should implement Niche
if its memory representation has any bit patterns that do not
represent valid values. If so, one of those can be used to represent the None
case of an
option.