rust-secure-code / mem-markers

Rust library for marker traits about types layout in memory
22 stars 1 forks source link

Sizeness Trait #5

Open HeroicKatora opened 4 years ago

HeroicKatora commented 4 years ago

There are some parts of the language with special-cased interaction based on the size information ot types. The plain core::mem::transmute requires the compiler to prove size-equivalence of two types, in a much less generic setting than the type bounds themselves suggest. The core::mem::transmute_copy requires the caller to unsafely uphold that the first parameter is not smaller than the second. And zero sized types (ZST) have additional properties:

These properties allow skipping or entirely eliding some validity checks. This is the size equivalent of Unaligned for alignment. For example, transmuting into a ZST can never violate its validity requirements and is always sound, although it might violate a type's safety requirements. This does not imply that all ZSTs implement ZeroSafe, which is also concerned with safety requirements.

Prior art

Lokathor commented 4 years ago

Small point of order: violating safety invariants isn't actually sound if you don't clean up your mess properly before things go wrong.

That's why the ZeroSafe proposal is about safe, not just valid.