@@ -79,19 +79,14 @@ impl<T: ?Sized> *const T {
7979 /// }
8080 /// ```
8181 #[ unstable( feature = "set_ptr_value" , issue = "75091" ) ]
82+ #[ rustc_const_unstable( feature = "set_ptr_value" , issue = "75091" ) ]
8283 #[ must_use = "returns a new pointer rather than modifying its argument" ]
8384 #[ inline]
84- pub fn with_metadata_of < U > ( self , mut val : * const U ) -> * const U
85+ pub const fn with_metadata_of < U > ( self , meta : * const U ) -> * const U
8586 where
8687 U : ?Sized ,
8788 {
88- let target = & mut val as * mut * const U as * mut * const u8 ;
89- // SAFETY: In case of a thin pointer, this operations is identical
90- // to a simple assignment. In case of a fat pointer, with the current
91- // fat pointer layout implementation, the first field of such a
92- // pointer is always the data pointer, which is likewise assigned.
93- unsafe { * target = self as * const u8 } ;
94- val
89+ from_raw_parts :: < U > ( self as * const ( ) , metadata ( meta) )
9590 }
9691
9792 /// Changes constness without changing the type.
@@ -478,8 +473,7 @@ impl<T: ?Sized> *const T {
478473 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
479474 pub const unsafe fn byte_offset ( self , count : isize ) -> Self {
480475 // SAFETY: the caller must uphold the safety contract for `offset`.
481- let this = unsafe { self . cast :: < u8 > ( ) . offset ( count) . cast :: < ( ) > ( ) } ;
482- from_raw_parts :: < T > ( this, metadata ( self ) )
476+ unsafe { self . cast :: < u8 > ( ) . offset ( count) . with_metadata_of ( self ) }
483477 }
484478
485479 /// Calculates the offset from a pointer using wrapping arithmetic.
@@ -559,7 +553,7 @@ impl<T: ?Sized> *const T {
559553 #[ unstable( feature = "pointer_byte_offsets" , issue = "96283" ) ]
560554 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "96283" ) ]
561555 pub const fn wrapping_byte_offset ( self , count : isize ) -> Self {
562- from_raw_parts :: < T > ( self . cast :: < u8 > ( ) . wrapping_offset ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
556+ self . cast :: < u8 > ( ) . wrapping_offset ( count) . with_metadata_of ( self )
563557 }
564558
565559 /// Masks out bits of the pointer according to a mask.
@@ -597,8 +591,7 @@ impl<T: ?Sized> *const T {
597591 #[ must_use = "returns a new pointer rather than modifying its argument" ]
598592 #[ inline( always) ]
599593 pub fn mask ( self , mask : usize ) -> * const T {
600- let this = intrinsics:: ptr_mask ( self . cast :: < ( ) > ( ) , mask) ;
601- from_raw_parts :: < T > ( this, metadata ( self ) )
594+ intrinsics:: ptr_mask ( self . cast :: < ( ) > ( ) , mask) . with_metadata_of ( self )
602595 }
603596
604597 /// Calculates the distance between two pointers. The returned value is in
@@ -939,8 +932,7 @@ impl<T: ?Sized> *const T {
939932 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
940933 pub const unsafe fn byte_add ( self , count : usize ) -> Self {
941934 // SAFETY: the caller must uphold the safety contract for `add`.
942- let this = unsafe { self . cast :: < u8 > ( ) . add ( count) . cast :: < ( ) > ( ) } ;
943- from_raw_parts :: < T > ( this, metadata ( self ) )
935+ unsafe { self . cast :: < u8 > ( ) . add ( count) . with_metadata_of ( self ) }
944936 }
945937
946938 /// Calculates the offset from a pointer (convenience for
@@ -1026,8 +1018,7 @@ impl<T: ?Sized> *const T {
10261018 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
10271019 pub const unsafe fn byte_sub ( self , count : usize ) -> Self {
10281020 // SAFETY: the caller must uphold the safety contract for `sub`.
1029- let this = unsafe { self . cast :: < u8 > ( ) . sub ( count) . cast :: < ( ) > ( ) } ;
1030- from_raw_parts :: < T > ( this, metadata ( self ) )
1021+ unsafe { self . cast :: < u8 > ( ) . sub ( count) . with_metadata_of ( self ) }
10311022 }
10321023
10331024 /// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1107,7 +1098,7 @@ impl<T: ?Sized> *const T {
11071098 #[ unstable( feature = "pointer_byte_offsets" , issue = "96283" ) ]
11081099 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "96283" ) ]
11091100 pub const fn wrapping_byte_add ( self , count : usize ) -> Self {
1110- from_raw_parts :: < T > ( self . cast :: < u8 > ( ) . wrapping_add ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
1101+ self . cast :: < u8 > ( ) . wrapping_add ( count) . with_metadata_of ( self )
11111102 }
11121103
11131104 /// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1187,7 +1178,7 @@ impl<T: ?Sized> *const T {
11871178 #[ unstable( feature = "pointer_byte_offsets" , issue = "96283" ) ]
11881179 #[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "96283" ) ]
11891180 pub const fn wrapping_byte_sub ( self , count : usize ) -> Self {
1190- from_raw_parts :: < T > ( self . cast :: < u8 > ( ) . wrapping_sub ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
1181+ self . cast :: < u8 > ( ) . wrapping_sub ( count) . with_metadata_of ( self )
11911182 }
11921183
11931184 /// Reads the value from `self` without moving it. This leaves the
0 commit comments