Skip to content

Commit

Permalink
Specify bit validity and padding of some types
Browse files Browse the repository at this point in the history
Specify the bit validity and padding of the primitive numeric 
types, bool, char, and pointer and reference types.

Closes rust-lang#1291
  • Loading branch information
joshlf authored Aug 11, 2023
1 parent f7e6f04 commit 510e75c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>()], 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::<T, [u8; size_of::<T>()]>(...) 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::<P, [u8; size_of::<P>()]>(...)` 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.
Expand Down

0 comments on commit 510e75c

Please sign in to comment.