Skip to content

Commit 403e09f

Browse files
Guarantee that raw pointer conversions preserve slice element count
This encodes the behavior agreed upon in rust-lang/unsafe-code-guidelines#288.
1 parent 7401b36 commit 403e09f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/type-coercions.md

+10
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ Coercion is allowed between the following types:
156156

157157
* `!` to any `T`
158158

159+
#### Slice DST raw pointer conversions
160+
161+
When `T` and `U` are both "slice DSTs" - ie, slice types or types whose trailing field
162+
is a slice type - the raw pointer types `*const T`, `*mut T`, `*const U`, and `*mut U`
163+
encode the number of elements in this slice. Coercions between these raw pointer types
164+
preserve the number of elements. Note that, as a consequence, such coercions do *not*
165+
necessarily preserve the size of the pointer's referent (e.g., coercing `*const [u16]`
166+
to `*const [u8]` will result in a raw pointer which refers to an object of half the size
167+
of the original).
168+
159169
### Unsized Coercions
160170

161171
The following coercions are called `unsized coercions`, since they

0 commit comments

Comments
 (0)