You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Enumeration | Integer type |[Enum cast][expr.as.enum]|
511
511
|`bool` or `char`| Integer type |[Primitive to integer cast][expr.as.bool-char-as-int]|
512
512
|`u8`|`char`|[`u8` to `char` cast][expr.as.u8-as-char]|
513
-
|`*T`|`*V`[^meta-compat]|[Pointer to pointer cast][expr.as.pointer]|
513
+
|`*T`|`*V`(when [compatible][expr.as.pointer]) |[Pointer to pointer cast][expr.as.pointer]|
514
514
|`*T` where `T: Sized`| Integer type |[Pointer to address cast][expr.as.pointer-as-int]|
515
515
| Integer type |`*V` where `V: Sized`|[Address to pointer cast][expr.as.int-as-pointer]|
516
516
|`&m₁ [T; n]`|`*m₂ T`[^lessmut]| Array to pointer cast |
@@ -522,13 +522,6 @@ r[expr.as.coercions]
522
522
|[Function pointer]| Integer | Function pointer to address cast |
523
523
| Closure [^no-capture]| Function pointer | Closure to function pointer cast |
524
524
525
-
[^meta-compat]: Where `T` and `V` have compatible metadata:
526
-
*`V: Sized`, or
527
-
* Both slice metadata (`*[u16]` -> `*[u8]`, `*str` -> `*(u8, [u32])`), or
528
-
* Both the same trait object metadata, modulo dropping auto traits (`*dyn Debug` -> `*(u16, dyn Debug)`, `*dyn Debug + Send` -> `*dyn Debug`)
529
-
***Note**: *adding* auto traits is only allowed if the principal trait has the auto trait as a super trait (given `trait T: Send {}`, `*dyn T` -> `*dyn T + Send` is valid, but `*dyn Debug` -> `*dyn Debug + Send` is not)
530
-
***Note**: Generics (including lifetimes) must match (`*dyn T<'a, A>` -> `*dyn T<'b, B>` requires `'a = 'b` and `A = B`)
531
-
532
525
[^lessmut]: Only when `m₁` is `mut` or `m₂` is `const`. Casting `mut` reference/pointer to `const` pointer is allowed.
533
526
534
527
[^no-capture]: Only closures that do not capture (close over) any local variables can be cast to function pointers.
- If `T` and `U` are both sized, the pointer is returned unchanged.
710
703
711
-
r[expr.as.pointer.unsized]
712
-
- If `T` and `U` are both unsized, the pointer is also returned unchanged. In particular, the metadata is preserved exactly.
713
-
714
-
For instance, a cast from `*const [T]` to `*const [U]` preserves 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)`.
0 commit comments