pub const unsafe fn conjure_zst<T>() -> TExpand description
Create a fresh instance of the inhabited ZST type T.
Prefer this to zeroed or uninitialized or transmute_copy
in places where you know that T is zero-sized, but donât have a bound
(such as Default) that would allow you to instantiate it using safe code.
If youâre not sure whether T is an inhabited ZST, then you should be
using MaybeUninit, not this function.
§Panics
If size_of::<T>() != 0.
§Safety
Tmust be inhabited, i.e. possible to construct. This means that types like zero-variant enums and!are unsound to conjure.- You must use the value only in ways which do not violate any safety invariants of the type.
While itâs easy to create a valid instance of an inhabited ZST, since having no bits in its representation means thereâs only one possible value, that doesnât mean that itâs always sound to do so.
For example, a library could design zero-sized tokens that are !Default + !Clone, limiting
their creation to functions that initialize some state or establish a scope. Conjuring such a
token could break invariants and lead to unsoundness.