-
Notifications
You must be signed in to change notification settings - Fork 9
Description
From rust-lang/rust#32838 (comment):
For me, an important constraint is that if we ban zero-sized allocations, the
Allocmethods should be able to assume that theLayoutpassed to them is not zero-sized.There are multiple ways to achieve this. One would be to add another
Safetyclause to allAllocmethods stating that if theLayoutis zero-sized the behavior is undefined.Alternatively, we could ban zero-sized
Layouts, thenAllocdoesn'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
HashMapbuild up theLayoutfrom multipleLayouts, and while the finalLayoutmight not be zero-sized, the intermediate ones might be (e.g. inHashSet). So these types would need to use "something else" (e.g. aLayoutBuildertype) to build up their finalLayouts, 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.