Option are special, since some nonnull optimizations (e.g.,
Option<&'static usize>) make it FFI safe and ABI compatible with other types (e.g.,
*const usize). But there are some problems:
- These nonnull optimizations have very few guarantees. For example, even though
Option<ManuallyDrop<&'static usize>>(I know
&'static usizedoesn’t need dropping; this is just a minimal example) doesn’t have any guarantees that it has the same size as
*const usize. I know in practice it does, but there’s a difference (at least at the semantic level) between what Rust does in practice and what it (publicly) guarantees.
- These other types aren’t FFI-safe and may not have the ABI that was intended. Continuing the same example above,
Option<ManuallyDrop<&'static usize>>is not FFI-safe and there aren’t guarantees it has the same ABI as
I would like to fix this. I would like to propose:
Option<T>is FFI-safe and ABI-compatible with
structtype that is
#[repr(transparent)]and it’s underlying representation is
Option<X>is FFI-safe and ABI-compatible with both
Additionally, this should extend to all
Option-like enums (not just the
Option provided by the std/core). Here, “
Option-like” would mean an enum that is eligible for nonnull optimizations such that
Enum<&'static usize> is FFI-safe and has the same ABI and representation as
Option<&'static usize> and
Are there any concerns with this? Is there a better way to formalize this?
I can draft an RFC if necessary.