From 13da89f6a813afb3ac718535b580b8c0adcf6278 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Thu, 15 Nov 2018 21:44:42 -0600 Subject: [PATCH 01/10] Added representation of pointer types. --- reference/src/representation/pointers.md | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 reference/src/representation/pointers.md diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md new file mode 100644 index 00000000..1a51f2d6 --- /dev/null +++ b/reference/src/representation/pointers.md @@ -0,0 +1,46 @@ +# Representation of reference and pointer types + +### Terminology + +Reference types are types of the form `&T` or `&mut T`. + +Raw pointer types are types of the form `*const T` or `*mut T`. + +### Representation + +The alignment of reference and raw pointer types is the word size. + +The sizes of `&T`, `&mut T`, `*const T` and `*mut T` are the same, +and are at least one word. + +* If `T` is a trait, then the size of `&T` is two words. +* If `T` is a sized type then the size of `&T` is one word. +* The size of `&[T]` is two words. + +Raw pointer types have no requirements of their representation. + +The representations of `&T` and `&mut T` are the same. + +The representation of `&T` when `T` is a trait is the same as that of: +```rust +#[repr(C)] +struct DynObject { + data: &u8, + vtable: &usize, +} +``` + +The representation of `&[T]` is the same as that of: +```rust +#[repr(C)] +struct Slice { + ptr: &T, + len: usize, +} +``` + +### Notes + +The validity requirements of `&T` include that all values are non-null, which +impacts niche optimizations and hence representation of types which include `&T`. +In particular, `Option<&T>` is one word. From 049dc62bfffaeb1751aa6a6c4cfcea01e1a095b3 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Thu, 29 Nov 2018 11:36:13 -0600 Subject: [PATCH 02/10] Moved same-representation discussion of fat pointers to non-normative text --- reference/src/representation/pointers.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index 1a51f2d6..6e08834f 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -17,7 +17,7 @@ and are at least one word. * If `T` is a sized type then the size of `&T` is one word. * The size of `&[T]` is two words. -Raw pointer types have no requirements of their representation. +### Notes The representations of `&T` and `&mut T` are the same. @@ -39,8 +39,6 @@ struct Slice { } ``` -### Notes - The validity requirements of `&T` include that all values are non-null, which impacts niche optimizations and hence representation of types which include `&T`. In particular, `Option<&T>` is one word. From ab527c75457c08e1d4cffa861158172f7bec4272 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Thu, 29 Nov 2018 17:19:02 -0600 Subject: [PATCH 03/10] Responding to review comments --- reference/src/representation/pointers.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index 6e08834f..9054de35 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -17,6 +17,9 @@ and are at least one word. * If `T` is a sized type then the size of `&T` is one word. * The size of `&[T]` is two words. +Note that we do not make any guarantees about the sizes of +multi-trait objects `&(dyn T + U)`or references to dynamically sized types. + ### Notes The representations of `&T` and `&mut T` are the same. @@ -38,7 +41,3 @@ struct Slice { len: usize, } ``` - -The validity requirements of `&T` include that all values are non-null, which -impacts niche optimizations and hence representation of types which include `&T`. -In particular, `Option<&T>` is one word. From 872a4056eba3e8c5d86998af43939525eb3a73a9 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Fri, 30 Nov 2018 08:56:45 -0600 Subject: [PATCH 04/10] Responding to review comments --- reference/src/representation/pointers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index 9054de35..bfb96163 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -29,7 +29,7 @@ The representation of `&T` when `T` is a trait is the same as that of: #[repr(C)] struct DynObject { data: &u8, - vtable: &usize, + vtable: &u8, } ``` From be860839a703c682766cc03ca74068c7055a9613 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Wed, 5 Dec 2018 14:59:24 -0600 Subject: [PATCH 05/10] Responding to review comments --- reference/src/representation/pointers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index bfb96163..d37be469 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -18,7 +18,7 @@ and are at least one word. * The size of `&[T]` is two words. Note that we do not make any guarantees about the sizes of -multi-trait objects `&(dyn T + U)`or references to dynamically sized types. +multi-trait objects `&(dyn T + U)`or references to other dynamically sized types. ### Notes From db5f98dce6b3a1aac31151b44c0407471a3e9055 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 10 Dec 2018 13:45:15 -0600 Subject: [PATCH 06/10] Update reference/src/representation/pointers.md Co-Authored-By: asajeffrey --- reference/src/representation/pointers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index d37be469..aa6c2549 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -18,7 +18,7 @@ and are at least one word. * The size of `&[T]` is two words. Note that we do not make any guarantees about the sizes of -multi-trait objects `&(dyn T + U)`or references to other dynamically sized types. +multi-trait objects `&(dyn T + U)` or references to other dynamically sized types. ### Notes From e2e6f0acbef017f2cca1fa6ea6b518bc06b707d8 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Mon, 10 Dec 2018 14:26:54 -0600 Subject: [PATCH 07/10] Responding to review comments --- reference/src/representation/pointers.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index aa6c2549..9d04f12f 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -2,29 +2,35 @@ ### Terminology -Reference types are types of the form `&T` or `&mut T`. +Reference types are types of the form `&T`, `&mut T` or `&dyn T`. Raw pointer types are types of the form `*const T` or `*mut T`. ### Representation -The alignment of reference and raw pointer types is the word size. +The alignment of `&T`, `&mut T`, `*const T` and `*mut T` are the same, +and are at least the word size. + +* If `T` is a trait, then the alignment of `&dyn T` is the word size. +* If `T` is a sized type then the alignment of `&T` is the word size. +* The alignment of `&[T]` is the word size. The sizes of `&T`, `&mut T`, `*const T` and `*mut T` are the same, and are at least one word. -* If `T` is a trait, then the size of `&T` is two words. +* If `T` is a trait, then the size of `&dyn T` is two words. * If `T` is a sized type then the size of `&T` is one word. * The size of `&[T]` is two words. -Note that we do not make any guarantees about the sizes of -multi-trait objects `&(dyn T + U)` or references to other dynamically sized types. - ### Notes The representations of `&T` and `&mut T` are the same. -The representation of `&T` when `T` is a trait is the same as that of: +We do not make any guarantees about the representation of +multi-trait objects `&(dyn T + U)` or references to other dynamically sized types, +other than that they are at least word-aligned, and have size at least one word. + +The representation of `&dyn T` when `T` is a trait is the same as that of: ```rust #[repr(C)] struct DynObject { From 45d6eeee848822cd74fef506d3a17442da6249b3 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Mon, 10 Dec 2018 14:27:29 -0600 Subject: [PATCH 08/10] Added text about &str --- reference/src/representation/pointers.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index 9d04f12f..d02b5346 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -14,6 +14,7 @@ and are at least the word size. * If `T` is a trait, then the alignment of `&dyn T` is the word size. * If `T` is a sized type then the alignment of `&T` is the word size. * The alignment of `&[T]` is the word size. +* The alignment of `&str` is the word size. The sizes of `&T`, `&mut T`, `*const T` and `*mut T` are the same, and are at least one word. @@ -21,6 +22,7 @@ and are at least one word. * If `T` is a trait, then the size of `&dyn T` is two words. * If `T` is a sized type then the size of `&T` is one word. * The size of `&[T]` is two words. +* The size of `&str` is two words. ### Notes @@ -47,3 +49,5 @@ struct Slice { len: usize, } ``` + +The representation of `&str` is the same as that of `&[u8]`. From 7fb3b937f3a1466749a20993b82af7a552da49c6 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Thu, 13 Dec 2018 10:55:43 -0600 Subject: [PATCH 09/10] Added bullet points making size/alignment clearer --- reference/src/representation/pointers.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index d02b5346..023a34d9 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -15,6 +15,7 @@ and are at least the word size. * If `T` is a sized type then the alignment of `&T` is the word size. * The alignment of `&[T]` is the word size. * The alignment of `&str` is the word size. +* Alignment in other cases may be more than the word size (e.g., for other dynamically sized types). The sizes of `&T`, `&mut T`, `*const T` and `*mut T` are the same, and are at least one word. @@ -23,6 +24,7 @@ and are at least one word. * If `T` is a sized type then the size of `&T` is one word. * The size of `&[T]` is two words. * The size of `&str` is two words. +* Size in other cases may be more than one word (e.g., for other dynamically sized types). ### Notes From fc3932053066242fe1f7522d47c3944c48b9d095 Mon Sep 17 00:00:00 2001 From: Alan Jeffrey Date: Thu, 13 Dec 2018 12:05:33 -0600 Subject: [PATCH 10/10] Use *T rather than &T --- reference/src/representation/pointers.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/reference/src/representation/pointers.md b/reference/src/representation/pointers.md index 023a34d9..2e4455ae 100644 --- a/reference/src/representation/pointers.md +++ b/reference/src/representation/pointers.md @@ -5,6 +5,7 @@ Reference types are types of the form `&T`, `&mut T` or `&dyn T`. Raw pointer types are types of the form `*const T` or `*mut T`. +We write `*T` when the mutability attribute is unimportant. ### Representation @@ -28,7 +29,7 @@ and are at least one word. ### Notes -The representations of `&T` and `&mut T` are the same. +The representations of `&T`, `&mut T` and `*T` are the same. We do not make any guarantees about the representation of multi-trait objects `&(dyn T + U)` or references to other dynamically sized types, @@ -38,8 +39,8 @@ The representation of `&dyn T` when `T` is a trait is the same as that of: ```rust #[repr(C)] struct DynObject { - data: &u8, - vtable: &u8, + data: *u8, + vtable: *u8, } ``` @@ -47,7 +48,7 @@ The representation of `&[T]` is the same as that of: ```rust #[repr(C)] struct Slice { - ptr: &T, + ptr: *T, len: usize, } ```