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
> // Cast from wide pointer (*const [i32]) to thin pointer (*const i32)
721
+
> // discarding the length metadata.
722
+
> letdata_ptr:*consti32=ptras*consti32;
723
+
> assert_eq!(unsafe { *data_ptr }, 1);
724
+
> ```
725
+
726
+
r[expr.as.pointer.unsized.unchanged]
708
727
-If `T` and `U` arebothunsized, thepointerisalsoreturnedunchanged.Inparticular, themetadataispreservedexactly.Thecastcanonlybeperformedifthemetadataiscompatibleaccordingtothebelowrules:
709
728
710
729
r[expr.as.pointer.unsized.slice]
711
730
-When `T` and `U` areunsizedwithslicemetadata, theyarealwayscompatible.Themetadataofasliceisthenumberofelements, socasting `*[u16] ->*[u8]` islegalbutwillresultinreducingthenumberofbytesbyhalf.
712
731
732
+
> [!EXAMPLE]
733
+
> ```rust
734
+
> letslice:&[u16] =&[1, 2, 3];
735
+
> letptr:*const [u16] =sliceas*const [u16];
736
+
> letbyte_ptr:*const [u8] =ptras*const [u8];
737
+
> assert_eq!(byte_ptr.len(), 3);
738
+
> ```
739
+
713
740
r[expr.as.pointer.unsized.trait]
714
741
-When `T` and `U` areunsizedwithtraitobjectmetadata, themetadataiscompatibleonlywhenallofthefollowingholds:
715
-
1. The principal trait must be the same. (you can't cast from `dyn Foo` to `dyn Bar`)
716
-
2. Auto traits may be removed. (you can cast `dyn Foo + Send` to `dyn Foo`)
717
-
3. Auto traits may be added only if they are a super trait of the principal trait. (you can cast `dyn Foo` to `dyn Foo + Send` only if `Send` is a super trait of `Foo`)
718
-
4. Trailing lifetimes may only be shortened. (you can cast `dyn Foo + 'long` to `dyn Foo + 'short`, but the opposite is not legal)
719
-
5. Generics (including lifetimes) and associated types must match exactly. (`*dyn T<'a, A>` -> `*dyn T<'b, B>` requires `'a = 'b` and `A = B`)
742
+
1.Theprincipaltraitmustbethesame.
743
+
744
+
> [!EXAMPLE]
745
+
> ```rust,compile_fail,E0606
746
+
> traitFoo {}
747
+
> traitBar {}
748
+
> implFoofori32 {}
749
+
> implBarfori32 {}
750
+
>
751
+
> letx:i32=42;
752
+
> letptr_foo:*constdynFoo=&xas*constdynFoo;
753
+
> // You can't cast to a different principal trait.
0 commit comments