(by the way, this is my first time submitting anything to Rust)
!!!!
This Pre-(Pre-)RFC is pretty much a moot point, due to its only real uses covered by existing types and c-like bitpacking not working with Rust's compiler model
!!!!
Edit 1: conservatively assume n8
s are uninhabited
Feature Name: niche_types Start Date: sometime in the future RFC PR/Rust Issue: N/A
Summary:
Adds a primitive, 1 byte sized, type with an undefined memory representation.
Motivation:
While talking on the Rust Community Discord, there came a need for bitpacking struct
s. As part of it, creating niches would be necessary. Niches are bit patterns within a struct or enum which can be repurposed by an enclosing enum or, in the future, an enclosing struct. While this Pre(-Pre)-RFC does not propose bitpacking, it provides a utility to partially control Rust's niches.
Guide-level explanation:
This Pre-(Pre-)RFC adds one type, n8
(name not final), which has no memory representation, but takes up one byte of space. The actual byte used depends on the contents: an Option<n8>
stores whether it is Some
or None
in the n8
's byte, meaning you CANNOT rely on its actual value. This means transmute
ing it to or from a u8
, optionally within pointers, references, and arrays, is always Undefined Behavior.
Reference-level explanation:
See above. (TODO)
Drawbacks:
Most likely uninhabited and unusable until the implementation of future ideas.
Rationale and alternatives
Some implementations of binary formats require precise control of not only the layout of data, but how Rust can reason about it. A u8
could be wrapped to obtain similar developer experience, but will not have a n8
's performance benefits. In that way, a n8
can be seen as an optimizing hint.
Prior Art
None, as Rust, to the best of my knowledge, has invented structure niches.
Unresolved questions
- Would a
n8
actually be constructable? What rules would need to be in place to construct one, or are they uninhabited? Ifn8
s turn out to be uninhabited, would it make sense, in current Rust, to add it?
TODO, suggest some
Future possibilities
For this to be useful, an abi guarantee would be needed to allow certain structures containing it to be inhabited.
This is a stepping stone towards bitpacked and bit-level struct
s, which need padding (which can be created using normal types) and niches within them.