|
| 1 | +# Representation of reference and pointer types |
| 2 | + |
| 3 | +### Terminology |
| 4 | + |
| 5 | +Reference types are types of the form `&T`, `&mut T` or `&dyn T`. |
| 6 | + |
| 7 | +Raw pointer types are types of the form `*const T` or `*mut T`. |
| 8 | +We write `*T` when the mutability attribute is unimportant. |
| 9 | + |
| 10 | +### Representation |
| 11 | + |
| 12 | +The alignment of `&T`, `&mut T`, `*const T` and `*mut T` are the same, |
| 13 | +and are at least the word size. |
| 14 | + |
| 15 | +* If `T` is a trait, then the alignment of `&dyn T` is the word size. |
| 16 | +* If `T` is a sized type then the alignment of `&T` is the word size. |
| 17 | +* The alignment of `&[T]` is the word size. |
| 18 | +* The alignment of `&str` is the word size. |
| 19 | +* Alignment in other cases may be more than the word size (e.g., for other dynamically sized types). |
| 20 | + |
| 21 | +The sizes of `&T`, `&mut T`, `*const T` and `*mut T` are the same, |
| 22 | +and are at least one word. |
| 23 | + |
| 24 | +* If `T` is a trait, then the size of `&dyn T` is two words. |
| 25 | +* If `T` is a sized type then the size of `&T` is one word. |
| 26 | +* The size of `&[T]` is two words. |
| 27 | +* The size of `&str` is two words. |
| 28 | +* Size in other cases may be more than one word (e.g., for other dynamically sized types). |
| 29 | + |
| 30 | +### Notes |
| 31 | + |
| 32 | +The representations of `&T`, `&mut T` and `*T` are the same. |
| 33 | + |
| 34 | +We do not make any guarantees about the representation of |
| 35 | +multi-trait objects `&(dyn T + U)` or references to other dynamically sized types, |
| 36 | +other than that they are at least word-aligned, and have size at least one word. |
| 37 | + |
| 38 | +The representation of `&dyn T` when `T` is a trait is the same as that of: |
| 39 | +```rust |
| 40 | +#[repr(C)] |
| 41 | +struct DynObject { |
| 42 | + data: *u8, |
| 43 | + vtable: *u8, |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +The representation of `&[T]` is the same as that of: |
| 48 | +```rust |
| 49 | +#[repr(C)] |
| 50 | +struct Slice<T> { |
| 51 | + ptr: *T, |
| 52 | + len: usize, |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +The representation of `&str` is the same as that of `&[u8]`. |
0 commit comments