Closed
Description
To my understanding, currently, a reference type has only one niche that is the null value, but a reference type must be properly aligned, which means in theory we can also utilize those unaligned values as niches. The total possible niches of a type can be calculated as:
fn reference_niches<T>() -> usize {
((MEMORY_SPACE_SIZE - size_of::<T>()) / align_of::<T>()).wrapping_neg()
}
where MEMORY_SPACE_SIZE
is the theoretical maximum size of the memory space in bytes.
Is it possible for Rust to enable this optimization?
Additionally, is it possible to add a new NonNullAligned<T>
type that has the same niches as &T
? We can use it to implement Box
, Rc
, Arc
and Vec
types, so they can also have the same niches.