@@ -78,23 +78,14 @@ impl<T: ?Sized> *mut T {
78
78
/// }
79
79
/// ```
80
80
#[ unstable( feature = "set_ptr_value" , issue = "75091" ) ]
81
+ #[ rustc_const_unstable( feature = "set_ptr_value" , issue = "75091" ) ]
81
82
#[ must_use = "returns a new pointer rather than modifying its argument" ]
82
83
#[ inline]
83
- pub fn with_metadata_of < U > ( self , val : * const U ) -> * mut U
84
+ pub const fn with_metadata_of < U > ( self , meta : * const U ) -> * mut U
84
85
where
85
86
U : ?Sized ,
86
87
{
87
- // Prepare in the type system that we will replace the pointer value with a mutable
88
- // pointer, taking the mutable provenance from the `self` pointer.
89
- let mut val = val as * mut U ;
90
- // Pointer to the pointer value within the value.
91
- let target = & mut val as * mut * mut U as * mut * mut u8 ;
92
- // SAFETY: In case of a thin pointer, this operations is identical
93
- // to a simple assignment. In case of a fat pointer, with the current
94
- // fat pointer layout implementation, the first field of such a
95
- // pointer is always the data pointer, which is likewise assigned.
96
- unsafe { * target = self as * mut u8 } ;
97
- val
88
+ from_raw_parts_mut :: < U > ( self as * mut ( ) , metadata ( meta) )
98
89
}
99
90
100
91
/// Changes constness without changing the type.
@@ -496,8 +487,7 @@ impl<T: ?Sized> *mut T {
496
487
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
497
488
pub const unsafe fn byte_offset ( self , count : isize ) -> Self {
498
489
// SAFETY: the caller must uphold the safety contract for `offset`.
499
- let this = unsafe { self . cast :: < u8 > ( ) . offset ( count) . cast :: < ( ) > ( ) } ;
500
- from_raw_parts_mut :: < T > ( this, metadata ( self ) )
490
+ unsafe { self . cast :: < u8 > ( ) . offset ( count) . with_metadata_of ( self ) }
501
491
}
502
492
503
493
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -576,10 +566,7 @@ impl<T: ?Sized> *mut T {
576
566
#[ unstable( feature = "pointer_byte_offsets" , issue = "96283" ) ]
577
567
#[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "96283" ) ]
578
568
pub const fn wrapping_byte_offset ( self , count : isize ) -> Self {
579
- from_raw_parts_mut :: < T > (
580
- self . cast :: < u8 > ( ) . wrapping_offset ( count) . cast :: < ( ) > ( ) ,
581
- metadata ( self ) ,
582
- )
569
+ self . cast :: < u8 > ( ) . wrapping_offset ( count) . with_metadata_of ( self )
583
570
}
584
571
585
572
/// Masks out bits of the pointer according to a mask.
@@ -620,8 +607,7 @@ impl<T: ?Sized> *mut T {
620
607
#[ must_use = "returns a new pointer rather than modifying its argument" ]
621
608
#[ inline( always) ]
622
609
pub fn mask ( self , mask : usize ) -> * mut T {
623
- let this = intrinsics:: ptr_mask ( self . cast :: < ( ) > ( ) , mask) as * mut ( ) ;
624
- from_raw_parts_mut :: < T > ( this, metadata ( self ) )
610
+ intrinsics:: ptr_mask ( self . cast :: < ( ) > ( ) , mask) . cast_mut ( ) . with_metadata_of ( self )
625
611
}
626
612
627
613
/// Returns `None` if the pointer is null, or else returns a unique reference to
@@ -1048,8 +1034,7 @@ impl<T: ?Sized> *mut T {
1048
1034
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
1049
1035
pub const unsafe fn byte_add ( self , count : usize ) -> Self {
1050
1036
// SAFETY: the caller must uphold the safety contract for `add`.
1051
- let this = unsafe { self . cast :: < u8 > ( ) . add ( count) . cast :: < ( ) > ( ) } ;
1052
- from_raw_parts_mut :: < T > ( this, metadata ( self ) )
1037
+ unsafe { self . cast :: < u8 > ( ) . add ( count) . with_metadata_of ( self ) }
1053
1038
}
1054
1039
1055
1040
/// Calculates the offset from a pointer (convenience for
@@ -1135,8 +1120,7 @@ impl<T: ?Sized> *mut T {
1135
1120
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
1136
1121
pub const unsafe fn byte_sub ( self , count : usize ) -> Self {
1137
1122
// SAFETY: the caller must uphold the safety contract for `sub`.
1138
- let this = unsafe { self . cast :: < u8 > ( ) . sub ( count) . cast :: < ( ) > ( ) } ;
1139
- from_raw_parts_mut :: < T > ( this, metadata ( self ) )
1123
+ unsafe { self . cast :: < u8 > ( ) . sub ( count) . with_metadata_of ( self ) }
1140
1124
}
1141
1125
1142
1126
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1216,7 +1200,7 @@ impl<T: ?Sized> *mut T {
1216
1200
#[ unstable( feature = "pointer_byte_offsets" , issue = "96283" ) ]
1217
1201
#[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "96283" ) ]
1218
1202
pub const fn wrapping_byte_add ( self , count : usize ) -> Self {
1219
- from_raw_parts_mut :: < T > ( self . cast :: < u8 > ( ) . wrapping_add ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
1203
+ self . cast :: < u8 > ( ) . wrapping_add ( count) . with_metadata_of ( self )
1220
1204
}
1221
1205
1222
1206
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -1296,7 +1280,7 @@ impl<T: ?Sized> *mut T {
1296
1280
#[ unstable( feature = "pointer_byte_offsets" , issue = "96283" ) ]
1297
1281
#[ rustc_const_unstable( feature = "const_pointer_byte_offsets" , issue = "96283" ) ]
1298
1282
pub const fn wrapping_byte_sub ( self , count : usize ) -> Self {
1299
- from_raw_parts_mut :: < T > ( self . cast :: < u8 > ( ) . wrapping_sub ( count) . cast :: < ( ) > ( ) , metadata ( self ) )
1283
+ self . cast :: < u8 > ( ) . wrapping_sub ( count) . with_metadata_of ( self )
1300
1284
}
1301
1285
1302
1286
/// Reads the value from `self` without moving it. This leaves the
0 commit comments