|
| 1 | +# Type Layout |
| 2 | + |
| 3 | +The layout of a type is the way the size, alignment, and the offsets of any |
| 4 | +fields and discriminants for the values of that type. |
| 5 | + |
| 6 | +While specific releases of the compiler will have the same layout for types, |
| 7 | +there is a lot of room for new versions of the compiler to do different things. |
| 8 | +Instead of trying to document exactly what is done, we only document what is |
| 9 | +guaranteed today. |
| 10 | + |
| 11 | +## Size and Alignment |
| 12 | + |
| 13 | +All values have an alignment and size. |
| 14 | + |
| 15 | +The *alignment* of a value specifies what addresses are valid to store the value |
| 16 | +at. A value of alignment `n` must only be stored at an address that is a |
| 17 | +multiple of n. For example, a value with an alignment of 2 must be stored at an |
| 18 | +even address, while a value with an alignment of 1 can be stored at any address. |
| 19 | +Alignment is measured in bytes, and must be at least 1, and always a power of 2. |
| 20 | +The alignment of a value can be checked with the [`align_of_val`] function. |
| 21 | + |
| 22 | +The *size* of a value is the offset in bytes between successive elements in an |
| 23 | +array with that item type including alignment padding. The size of a value is |
| 24 | +always a multiple of its alignment. The size of a value can be checked with the |
| 25 | +[`size_of_val`] function. |
| 26 | + |
| 27 | +Types where all values have the same size and alignment known at compile time |
| 28 | +implement the [`Sized`] trait and can be checked with the [`size_of`] and |
| 29 | +[`align_of`] functions. Types that are not [`Sized`] are known as [dynamically |
| 30 | +sized types]. Since all values of a `Sized` type share the same size and |
| 31 | +alignment, we refer to those shared values as the size of the type and the |
| 32 | +alignment of the type respectively. |
| 33 | + |
| 34 | +## Primitive Data Layout |
| 35 | + |
| 36 | +The size of most primitives is given in this table. |
| 37 | + |
| 38 | +Type | `size_of::\<Type>()` |
| 39 | +- | - | - |
| 40 | +bool | 1 |
| 41 | +u8 | 1 |
| 42 | +u16 | 2 |
| 43 | +u32 | 4 |
| 44 | +u64 | 8 |
| 45 | +i8 | 1 |
| 46 | +i16 | 2 |
| 47 | +i32 | 4 |
| 48 | +i64 | 8 |
| 49 | +f32 | 4 |
| 50 | +f64 | 8 |
| 51 | +char | 4 |
| 52 | + |
| 53 | +`usize` and `isize` have a size big enough to contain every address on the |
| 54 | +target platform. For example, on a 32 bit target, this is 4 bytes and on a 64 |
| 55 | +bit target, this is 8 bytes. |
| 56 | + |
| 57 | +Most primitives are generally aligned to their size, although this is |
| 58 | +platform-specific behavior. In particular, on x86 u64 and f64 may be only |
| 59 | +aligned to 32 bits. |
| 60 | + |
| 61 | +## Pointers and References Layout |
| 62 | + |
| 63 | +Pointers and references have the same layout. Mutability of the pointer or |
| 64 | +reference does not change the layout. |
| 65 | + |
| 66 | +Pointers to sized types have the same size and alignment as `usize`. |
| 67 | + |
| 68 | +Pointers to unsized types are sized. The size and alignemnt is guaranteed to be |
| 69 | +at least equal to the size and alignment of a pointer. |
| 70 | + |
| 71 | +> Note: Though you should not rely on this, all pointers to <abbr |
| 72 | +> title="Dynamically Sized Types">DSTs</abbr> are currently twice the size of |
| 73 | +> the size of `usize` and have the same alignment. |
| 74 | +
|
| 75 | +## Array Layout |
| 76 | + |
| 77 | +Arrays are laid out so that the `nth` element of the array is offset from the |
| 78 | +start of the array by `n * the size of the type` bytes. An array of `[T; n]` |
| 79 | +has a size of `size_of::<T>() * n` and the same alignment of `T`. |
| 80 | + |
| 81 | +## Slice Layout |
| 82 | + |
| 83 | +Slices have the same layout as the section of the array they slice. |
| 84 | + |
| 85 | +## Tuple Layout |
| 86 | + |
| 87 | +Tuples do not have any guarantes about their layout. |
| 88 | + |
| 89 | +The exception to this is the unit tuple (`()`) which is guaranteed as a |
| 90 | +zero-sized type to have a size of 0 and an alignment of 1. |
| 91 | + |
| 92 | +## Trait Object Layout |
| 93 | + |
| 94 | +Trait objects have the same layout as the value the trait object is of. |
| 95 | + |
| 96 | +## Closure Layout |
| 97 | + |
| 98 | +Closures have no layout guarantees. |
| 99 | + |
| 100 | +## Representations |
| 101 | + |
| 102 | +All user-defined composite types (`struct`s, `enum`, and `union`s) have a |
| 103 | +*representation* that specifies what the layout is for the type. |
| 104 | + |
| 105 | +> Note: The representation does not depend upon the type's fields or generic |
| 106 | +> parameters. |
| 107 | +
|
| 108 | +The possible representations for a type are the default representation, `C`, the |
| 109 | +primitive representations, and `packed`. Multiple representations can be applied |
| 110 | +to a single type. |
| 111 | + |
| 112 | +The representation of a type can be changed by applying the [`repr` attribute] |
| 113 | +to it. The following example shows a struct with a `C` representation. |
| 114 | + |
| 115 | +``` |
| 116 | +#[repr(C)] |
| 117 | +struct ThreeInts { |
| 118 | + first: i16, |
| 119 | + second: i8, |
| 120 | + third: i32 |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +The representation of a type does not change the layout of its fields. For |
| 125 | +example, a struct with a `C` representation that contains a struct `Inner` with |
| 126 | +the default representation will not change the layout of Inner. |
| 127 | + |
| 128 | +### The Default Representation |
| 129 | + |
| 130 | +Nominal types without a `repr` attribute have the default representation. |
| 131 | +Informally, this representation is also called the `rust` representation. |
| 132 | + |
| 133 | +There are no guarantees of data layout made by this representation. |
| 134 | + |
| 135 | +### The `C` Representation |
| 136 | + |
| 137 | +The `C` representation is designed for creating types that are interoptable with |
| 138 | +the C Language and soundly performing operations that rely on data layout such |
| 139 | +as reinterpreting values as a different type. |
| 140 | + |
| 141 | +This representation can be applied to structs, unions, and enums. |
| 142 | + |
| 143 | +#### \#[repr(C)] Structs |
| 144 | + |
| 145 | +The alignment of the struct is the alignment of the most-aligned field in it. |
| 146 | + |
| 147 | +The size and offset of fields is determine by the following algorithm. |
| 148 | + |
| 149 | +Start with a current offset of 0 bytes. |
| 150 | + |
| 151 | +For each field in declaration order in the struct, first determine the size and |
| 152 | +alignment of the field. If the current offset is not a multiple of the field's |
| 153 | +alignment, then add padding bytes increasing the current offset until the |
| 154 | +current offset is a multiple of the field's alignment. The offset for the field |
| 155 | +is what the current offset is now. Then increase the current offset by the size |
| 156 | +of the field. |
| 157 | + |
| 158 | +Finally, the size of the struct is the current offset rounded up to the nearest |
| 159 | +multiple of the struct's alignment. |
| 160 | + |
| 161 | +> Note: You can have zero-sized structs from this algorithm. This differs from |
| 162 | +> C where structs without data still have a size of one byte. |
| 163 | +
|
| 164 | +#### \#[repr(C)] Unions |
| 165 | + |
| 166 | +A union declared with `#[repr(C)]` will have the same size and alignment as an |
| 167 | +equivalent C union declaration in the C language for the target platform. |
| 168 | +Usually, a union would have the maximum size of the maximum size of all of its |
| 169 | +fields, and the maximum alignment of the maximum alignment of all of its fields. |
| 170 | +These maximums may come from different fields. |
| 171 | + |
| 172 | +``` |
| 173 | +#[repr(C)] |
| 174 | +union Union { |
| 175 | + f1: u16, |
| 176 | + f2: [u8; 4], |
| 177 | +} |
| 178 | +
|
| 179 | +assert_eq!(std::mem::size_of::<Union>(), 4); // From f2 |
| 180 | +assert_eq!(std::mem::align_of::<Union>(), 2); // From f1 |
| 181 | +``` |
| 182 | + |
| 183 | +#### \#[repr(C)] Enums |
| 184 | + |
| 185 | +For [C-like enumerations], the `C` representation has the size and alignment of |
| 186 | +the default `enum` size and alignment for the target platform's C ABI. |
| 187 | + |
| 188 | +> Note: The enum representation in C is implementation defined, so this is |
| 189 | +> really a "best guess". In particular, this may be incorrect when the C code |
| 190 | +> of interest is compiled with certain flags. |
| 191 | +
|
| 192 | +> Warning: There are crucial differences between an `enum` in the C language and |
| 193 | +> Rust's C-like enumerations with this representation. An `enum` in C is |
| 194 | +> mostly a `typedef` plus some named constants; in other words, an object of an |
| 195 | +> `enum` type can hold any integer value. For example, this is often used for |
| 196 | +> bitflags in `C`. In contrast, Rust’s C-like enumerations can only legally hold |
| 197 | +> the discrimnant values, everything else is undefined behaviour. Therefore, |
| 198 | +> using a C-like enumeration in FFI to model a C `enum` is often wrong. |
| 199 | +
|
| 200 | +It is an error for [zero-variant enumerations] to have the `C` representation. |
| 201 | + |
| 202 | +For all other enumerations, the layout is unspecified. |
| 203 | + |
| 204 | +### Primitive representations |
| 205 | + |
| 206 | +The *primitive representations* are the representations with the same names as |
| 207 | +the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `usize`, `i8`, |
| 208 | +`i16`, `i32`, `i64`, and `isize`. |
| 209 | + |
| 210 | +Primitive representations can only be applied to enumerations. |
| 211 | + |
| 212 | +For [C-like enumerations], they set the size and alignment to be the same as the |
| 213 | +primitive type of the same name. For example, a C-like enumeration with a `u8` |
| 214 | +representation can only have discriminants between 0 and 255 inclusive. |
| 215 | + |
| 216 | +It is an error for [zero-variant enumerations] to have a primitive |
| 217 | +representation. |
| 218 | + |
| 219 | +For all other enumerations, the layout is unspecified. |
| 220 | + |
| 221 | +### The `packed` Representation |
| 222 | + |
| 223 | +The `packed` representation can only be used on `struct`s and `union`s. |
| 224 | + |
| 225 | +It modifies the representation (either the default or `C`) by removing any |
| 226 | +padding bytes and forcing the alignment of the type to `1`. |
| 227 | + |
| 228 | +> Warning: Dereferencing an unaligned pointer is [undefined behaviour] and is |
| 229 | +> possible to [safely create unaligned pointers to `packed` fields][27060]. |
| 230 | +> Like all ways to create undefined behavior in safe Rust, this is a bug. |
| 231 | +
|
| 232 | +[`align_of_val`]: ../std/mem/fn.align_of_val.html |
| 233 | +[`size_of_val`]: ../std/mem/fn.size_of_val.html |
| 234 | +[`align_of`]: ../std/mem/fn.align_of.html |
| 235 | +[`size_of`]: ../std/mem/fn.size_of.html |
| 236 | +[`Sized`]: ../std/marker/trait.Sized.html |
| 237 | +[dynamically sized types]: dynamically-sized-types.html |
| 238 | +[C-like enumerations]: items/enumerations.html#c-like-enumerations |
| 239 | +[zero-variant enumerations]: items/enumerations.html#zero-variant-enumerations |
| 240 | +[undefined behavior]: behavior-considered-undefined.html |
| 241 | +[27060]: https://github.com/rust-lang/rust/issues/27060 |
0 commit comments