Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b22559f

Browse files
Rollup merge of #103346 - HeroicKatora:metadata_of_const_pointer_argument, r=dtolnay
Adjust 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. In some cases, the current parameter type can thus lead to a very slightly confusing additional cast. [Example](197g/static-alloc@cad9377). ```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); ``` Tracking issue: #75091 ``@dtolnay`` you're reviewed #95249, would you mind chiming in?
2 parents 3f49f95 + e3606b2 commit b22559f

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

‎library/alloc/src/rc.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ impl<T: ?Sized> Rc<T> {
13861386
Self::allocate_for_layout(
13871387
Layout::for_value(&*ptr),
13881388
|layout| Global.allocate(layout),
1389-
|mem| mem.with_metadata_of(ptr as *mut RcBox<T>),
1389+
|mem| mem.with_metadata_of(ptr as *const RcBox<T>),
13901390
)
13911391
}
13921392
}

‎library/alloc/src/sync.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ impl<T: ?Sized> Arc<T> {
12041204
Self::allocate_for_layout(
12051205
Layout::for_value(&*ptr),
12061206
|layout| Global.allocate(layout),
1207-
|mem| mem.with_metadata_of(ptr as *mut ArcInner<T>),
1207+
|mem| mem.with_metadata_of(ptr as *const ArcInner<T>),
12081208
)
12091209
}
12101210
}

‎library/core/src/ptr/mut_ptr.rs‎

Lines changed: 5 additions & 1 deletion
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, mutval: *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)

AltStyle によって変換されたページ (->オリジナル) /