From 8ce11062bc09f7bafa07d31b8379bf33236078a8 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 11 Oct 2023 08:37:07 -0700 Subject: [PATCH 1/7] Guarantee that raw pointer conversions preserve slice element count This encodes the behavior agreed upon in https://github.com/rust-lang/unsafe-code-guidelines/issues/288. --- src/type-coercions.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/type-coercions.md b/src/type-coercions.md index 45d81d386..8edf98607 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -156,6 +156,16 @@ Coercion is allowed between the following types: * `!` to any `T` +#### Slice DST raw pointer conversions + +When `T` and `U` are both "slice DSTs" - ie, slice types or types whose trailing field +is a slice type - the raw pointer types `*const T`, `*mut T`, `*const U`, and `*mut U` +encode the number of elements in this slice. Coercions between these raw pointer types +preserve the number of elements. Note that, as a consequence, such coercions do *not* +necessarily preserve the size of the pointer's referent (e.g., coercing `*const [u16]` +to `*const [u8]` will result in a raw pointer which refers to an object of half the size +of the original). + ### Unsized Coercions The following coercions are called `unsized coercions`, since they From b6089dda0e9f190ef3607a7213f129091e87bde9 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 11 Oct 2023 09:07:38 -0700 Subject: [PATCH 2/7] Update operator-expr.md --- src/expressions/operator-expr.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 8b6429636..07007788d 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -478,6 +478,16 @@ unsafe { assert_eq!(values[1], 3); ``` +#### Slice DST pointer to pointer cast + +When `T` and `U` are both "slice DSTs" - ie, slice types or types whose trailing field +is a slice type - the raw pointer types `*const T`, `*mut T`, `*const U`, and `*mut U` +encode the number of elements in this slice. Casts between these raw pointer types +preserve the number of elements. Note that, as a consequence, such casts do *not* +necessarily preserve the size of the pointer's referent (e.g., casting `*const [u16]` +to `*const [u8]` will result in a raw pointer which refers to an object of half the size +of the original). + ## Assignment expressions > **Syntax**\ From 6dc7d633c507d74877f71a6dd6b314b179ee0800 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 11 Oct 2023 09:08:09 -0700 Subject: [PATCH 3/7] Update type-coercions.md --- src/type-coercions.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/type-coercions.md b/src/type-coercions.md index 8edf98607..45d81d386 100644 --- a/src/type-coercions.md +++ b/src/type-coercions.md @@ -156,16 +156,6 @@ Coercion is allowed between the following types: * `!` to any `T` -#### Slice DST raw pointer conversions - -When `T` and `U` are both "slice DSTs" - ie, slice types or types whose trailing field -is a slice type - the raw pointer types `*const T`, `*mut T`, `*const U`, and `*mut U` -encode the number of elements in this slice. Coercions between these raw pointer types -preserve the number of elements. Note that, as a consequence, such coercions do *not* -necessarily preserve the size of the pointer's referent (e.g., coercing `*const [u16]` -to `*const [u8]` will result in a raw pointer which refers to an object of half the size -of the original). - ### Unsized Coercions The following coercions are called `unsized coercions`, since they From 2fe40f8ae5e822aa1f8065ab0d513e3cba16cd47 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 11 Oct 2023 10:04:53 -0700 Subject: [PATCH 4/7] Update operator-expr.md --- src/expressions/operator-expr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 07007788d..31d8f8236 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -480,7 +480,7 @@ assert_eq!(values[1], 3); #### Slice DST pointer to pointer cast -When `T` and `U` are both "slice DSTs" - ie, slice types or types whose trailing field +When `T` and `U` are both "slice DSTs" - i.e., slice types or types whose trailing field is a slice type - the raw pointer types `*const T`, `*mut T`, `*const U`, and `*mut U` encode the number of elements in this slice. Casts between these raw pointer types preserve the number of elements. Note that, as a consequence, such casts do *not* From ed4811da2da6dd56784a7f174dca7afdc14588d1 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 25 Oct 2023 21:42:18 -0700 Subject: [PATCH 5/7] Update src/expressions/operator-expr.md Co-authored-by: Niko Matsakis --- src/expressions/operator-expr.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 31d8f8236..f84b87219 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -480,8 +480,7 @@ assert_eq!(values[1], 3); #### Slice DST pointer to pointer cast -When `T` and `U` are both "slice DSTs" - i.e., slice types or types whose trailing field -is a slice type - the raw pointer types `*const T`, `*mut T`, `*const U`, and `*mut U` +For slice types like `[T]` and `[U]`, the raw pointer types `*const [T]`, `*mut [T]`, `*const [U]`, and `*mut [U]` encode the number of elements in this slice. Casts between these raw pointer types preserve the number of elements. Note that, as a consequence, such casts do *not* necessarily preserve the size of the pointer's referent (e.g., casting `*const [u16]` From b3217718e448ad8d3f084b590ece460d1f6d139e Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 25 Oct 2023 21:43:21 -0700 Subject: [PATCH 6/7] Update operator-expr.md --- src/expressions/operator-expr.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index f84b87219..1fbdb3a94 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -480,12 +480,15 @@ assert_eq!(values[1], 3); #### Slice DST pointer to pointer cast -For slice types like `[T]` and `[U]`, the raw pointer types `*const [T]`, `*mut [T]`, `*const [U]`, and `*mut [U]` -encode the number of elements in this slice. Casts between these raw pointer types -preserve the number of elements. Note that, as a consequence, such casts do *not* -necessarily preserve the size of the pointer's referent (e.g., casting `*const [u16]` -to `*const [u8]` will result in a raw pointer which refers to an object of half the size -of the original). +For slice types like `[T]` and `[U]`, the raw pointer types `*const [T]`, `*mut [T]`, +`*const [U]`, and `*mut [U]` encode the number of elements in this slice. Casts between +these raw pointer types preserve the number of elements. Note that, as a consequence, +such casts do *not* necessarily preserve the size of the pointer's referent (e.g., +casting `*const [u16]` to `*const [u8]` will result in a raw pointer which refers to an +object of half the size of the original). The same holds for `str` and any compound type +whose unsized tail is a slice type, such as struct `Foo(i32, [u8])` or `(u64, Foo)`. + + ## Assignment expressions From ad09bb0675a22cd453cfdec58da24e4c407d15a8 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 4 Nov 2023 10:18:23 -0700 Subject: [PATCH 7/7] Remove extra blank lines. --- src/expressions/operator-expr.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 1fbdb3a94..bd4998af5 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -488,8 +488,6 @@ casting `*const [u16]` to `*const [u8]` will result in a raw pointer which refer object of half the size of the original). The same holds for `str` and any compound type whose unsized tail is a slice type, such as struct `Foo(i32, [u8])` or `(u64, Foo)`. - - ## Assignment expressions > **Syntax**\