From 49aa9bf6f699ebdc65092be26ad18e130d38d243 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Mon, 11 Mar 2024 15:35:31 -0700 Subject: [PATCH 1/3] [type-layout] Document minimum size and alignment --- src/type-layout.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/type-layout.md b/src/type-layout.md index d338eb7bc..b69589ac2 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -32,6 +32,28 @@ as [dynamically sized types]. Since all values of a `Sized` type share the same size and alignment, we refer to those shared values as the size of the type and the alignment of the type respectively. +### Minimum Size and Alignment + +Every type, including [dynamically sized types], has a "minimum size" and "minimum +alignment." Every value of a type must have a size at least as large as the type's +minimum size and an alignment at least as large as the type's minimum alignment. +Some notable cases of minimum size and alignment are: +- For [`Sized`] types, the type's minimum size and minimum alignment + are always equal to the type's size and alignment +- For a [slice type](#slice-layout), `[T]`, the minimum size is 0 bytes (corresponding + a slice with 0 elements), and the minimum alignment is the alignment of `T` +- For a [trait object](#trait-object-layout), the minimum size is 0 and the minimum + alignment is 1 +- For a struct type with a dynamically-sized field, the minimum size is taken to be + the size of the struct when the dynamically-sized field has *its* minimum size. + The struct's minimum alignment is taken to be the alignment of the struct when the + dynamically-sized field has *its* minimum alignment. When relying on this property + of dynamically-sized structs, be careful not to assume too much! For example, + this property provides *no* guarantees about [`repr(Rust)`](#the-rust-representation) + types, as such a type can have arbitrarily large size and alignment regardless of + the sizes and alignments of its fields +- Every type's minimum size is less than or equal to `isize::MAX` + ## Primitive Data Layout The size of most primitives is given in this table. From dcc71632f452c1f807c6b294fdbd591a20251b05 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Mon, 11 Mar 2024 15:37:54 -0700 Subject: [PATCH 2/3] Update type-layout.md --- src/type-layout.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index b69589ac2..c5ce7a3d0 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -39,9 +39,9 @@ alignment." Every value of a type must have a size at least as large as the type minimum size and an alignment at least as large as the type's minimum alignment. Some notable cases of minimum size and alignment are: - For [`Sized`] types, the type's minimum size and minimum alignment - are always equal to the type's size and alignment + are always equal to the type's size and alignment, respectively - For a [slice type](#slice-layout), `[T]`, the minimum size is 0 bytes (corresponding - a slice with 0 elements), and the minimum alignment is the alignment of `T` + to a slice with 0 elements), and the minimum alignment is the alignment of `T` - For a [trait object](#trait-object-layout), the minimum size is 0 and the minimum alignment is 1 - For a struct type with a dynamically-sized field, the minimum size is taken to be From 8fb1ce0dad13426669f06eac3d5471c7bf0a4dee Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Sat, 11 May 2024 11:21:20 -0700 Subject: [PATCH 3/3] Update type-layout.md based on Ralf's feedback --- src/type-layout.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index c5ce7a3d0..6cc920014 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -45,14 +45,15 @@ Some notable cases of minimum size and alignment are: - For a [trait object](#trait-object-layout), the minimum size is 0 and the minimum alignment is 1 - For a struct type with a dynamically-sized field, the minimum size is taken to be - the size of the struct when the dynamically-sized field has *its* minimum size. - The struct's minimum alignment is taken to be the alignment of the struct when the - dynamically-sized field has *its* minimum alignment. When relying on this property - of dynamically-sized structs, be careful not to assume too much! For example, - this property provides *no* guarantees about [`repr(Rust)`](#the-rust-representation) - types, as such a type can have arbitrarily large size and alignment regardless of - the sizes and alignments of its fields -- Every type's minimum size is less than or equal to `isize::MAX` + the size of the struct when the dynamically-sized field has *its* minimum size and + its minimum alignment. The struct's minimum alignment is taken to be the alignment + of the struct when the dynamically-sized field has *its* minimum alignment. When + relying on this property of dynamically-sized structs, be careful not to assume too + much! For example, this property provides *no* guarantees about + [`repr(Rust)`](#the-rust-representation) types, as such a type can have arbitrarily + large size and alignment regardless of the sizes and alignments of its fields + +Every type's minimum size is less than or equal to `isize::MAX`. ## Primitive Data Layout