Skip to content

Commit 71c39de

Browse files
author
Andreas Molzer
committed
Argument type for mutable with_metadata_of (#75091)
The method takes two pointer arguments: one `self` supplying the pointer value, and a second pointer supplying the metadata. The new parameter type more clearly reflects the actual requirements. The provenance of the metadata parameter is disregarded completely. Using a mutable pointer in the call site can be coerced to a const pointer while the reverse is not true. An example of the current use: ```rust // Manually taking an unsized object from a `ManuallyDrop` into another allocation. let val: &core::mem::ManuallyDrop<T> = …; let ptr = val as *const _ as *mut T; let ptr = uninit.as_ptr().with_metadata_of(ptr); ``` This could then instead be simplified to: ```rust // Manually taking an unsized object from a `ManuallyDrop` into another allocation. let val: &core::mem::ManuallyDrop<T> = …; let ptr = uninit.as_ptr().with_metadata_of(&**val); ```
1 parent b1ab3b7 commit 71c39de

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/core/src/ptr/mut_ptr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ impl<T: ?Sized> *mut T {
8080
#[unstable(feature = "set_ptr_value", issue = "75091")]
8181
#[must_use = "returns a new pointer rather than modifying its argument"]
8282
#[inline]
83-
pub fn with_metadata_of<U>(self, mut val: *mut U) -> *mut U
83+
pub fn with_metadata_of<U>(self, val: *const U) -> *mut U
8484
where
8585
U: ?Sized,
8686
{
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.
8791
let target = &mut val as *mut *mut U as *mut *mut u8;
8892
// SAFETY: In case of a thin pointer, this operations is identical
8993
// to a simple assignment. In case of a fat pointer, with the current

0 commit comments

Comments
 (0)