From 5d9c879a94df0533ca276a9c3c74a7942e2cc684 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 24 Jan 2024 19:31:33 +0100 Subject: [PATCH] Trim down and remove anything superflous --- src/expressions/operator-expr.md | 49 ++++++-------------------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 785665da5..1c8a6de39 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -478,48 +478,17 @@ unsafe { assert_eq!(values[1], 3); ``` -#### Sized to sized pointer casts +#### Pointer-to-pointer cast -A sized pointer to pointer cast `*const T as *const U` or `*mut T as *mut U` -where `T: Sized` and `U: Sized` produces a value identical to the original -pointer. +`*const T` / `*mut T` can be cast to `*const U` / `*mut U` under the following +conditions: -#### Unsized to unsized pointer casts - -Any unsized to unsized pointer cast `*const T as *const U` or `*mut T as *mut U` -produces a value identical to the original pointer if `T` and `U` are compatible -unsized types. - -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)`. - -#### Unsized to sized pointer casts - -Any wide to narrow pointer cast discards all metadata that completes the wide -pointer and produces a thin base pointer. - -The following casts are well-defined: - -* `*const [T] to *const U` or `*mut [T] to *mut U` discards the length of the - slice and produces a pointer to the first element. The produced pointer is - casted to `U` in the same manner as a sized pointer to pointer cast. -* `*const dyn T to *const U` or `*mut dyn T to *mut U` discards all metadata - associated with the [trait object] and produces a pointer to the instance from - which the trait object was constructed. The produced pointer is casted to `U` - in the same manner as a sized pointer to pointer cast. - -For example: - -* `*const [u32] as *const u8` would produce a pointer to the first element - constituting the slice, casted to a `*const u8` pointer. -* `*const str` and `*mut str` have the same semantics as `*const [u8]` and `*mut - [u8]` since they have [identical layouts][_str_layout_]. +- If both `T` and `U` are sized, the cast produces a value identical to the + original pointer. +- If both `T` and `U` are unsized, the cast produces a value identical to the + original pointer if `T` and `U` are compatible unsized types. +- If `T` is unsized and `U` is sized, the cast discards all metadata that + completes the wide pointer `T` and produces a thin base pointer `U`. ## Assignment expressions