From 510e75c2a11386f8f69d044b4c93d2f6471ace80 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Thu, 10 Aug 2023 21:54:35 -0700 Subject: [PATCH] Specify bit validity and padding of some types Specify the bit validity and padding of the primitive numeric types, bool, char, and pointer and reference types. Closes #1291 --- src/type-layout.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/type-layout.md b/src/type-layout.md index 4c87954f3..b304f64f3 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -56,12 +56,23 @@ Most primitives are generally aligned to their size, although this is platform-specific behavior. In particular, on x86 u64 and f64 are only aligned to 32 bits. +For the primitive numeric types (`u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, +`i64`, `u128`, `i128`, `usize`, `isize`, `f32`, and `f64`), every bit pattern +represents a valid instance of the type (in other words, +`transmute::<[u8; size_of::()], T>(...)` is always sound). For the primitive +numeric types and also for `bool` and `char`, every byte is guaranteed to be +initialized (in other words, `transmute::()]>(...) is always +sound). + ## Pointers and References Layout Pointers and references have the same layout. Mutability of the pointer or reference does not change the layout. -Pointers to sized types have the same size and alignment as `usize`. +Pointers to sized types have the same size and alignment as `usize`. Every +byte of a pointer to a sized type and of a reference to a sized type is +initialized (in other words, for such a pointer or reference type, `P`, +`transmute::()]>(...)` is always sound). Pointers to unsized types are sized. The size and alignment is guaranteed to be at least equal to the size and alignment of a pointer.