Description
From rust-lang/rust#32838 (comment):
For me, an important constraint is that if we ban zero-sized allocations, the
Alloc
methods should be able to assume that theLayout
passed to them is not zero-sized.There are multiple ways to achieve this. One would be to add another
Safety
clause to allAlloc
methods stating that if theLayout
is zero-sized the behavior is undefined.Alternatively, we could ban zero-sized
Layout
s, thenAlloc
doesn't need to say anything about zero-sized allocations since these cannot safely happen, but doing that would have some downsides.For example, , some types like
HashMap
build up theLayout
from multipleLayout
s, and while the finalLayout
might not be zero-sized, the intermediate ones might be (e.g. inHashSet
). So these types would need to use "something else" (e.g. aLayoutBuilder
type) to build up their finalLayout
s, and pay for a "non-zero-sized" check (or use an_unchecked
) method when converting toLayout
.
Making Layout
only accept non-zero size is not possible anymore as it's stable. But banning zero-size allocation would simplify the safety clause on Alloc
.
This would also unlock to move helper methods to an extension trait.
This change could easily be reverted in the future if actual use cases appear. Currently I don't see any, because we cannot rely on an implementation to allow zero-sized allocations, thus we have to check size_of::<T>() == 0
in any way.