File tree 2 files changed +6
-20
lines changed
2 files changed +6
-20
lines changed Original file line number Diff line number Diff line change @@ -79,19 +79,14 @@ impl<T: ?Sized> *const T {
79
79
/// }
80
80
/// ```
81
81
#[ unstable( feature = "set_ptr_value" , issue = "75091" ) ]
82
+ #[ rustc_const_unstable( feature = "set_ptr_value" , issue = "75091" ) ]
82
83
#[ must_use = "returns a new pointer rather than modifying its argument" ]
83
84
#[ 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
85
86
where
86
87
U : ?Sized ,
87
88
{
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) )
95
90
}
96
91
97
92
/// Changes constness without changing the type.
Original file line number Diff line number Diff line change @@ -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.
You can’t perform that action at this time.
0 commit comments