From d489ea777d38c5d44a5394bcd4bdcc69eb3cda40 Mon Sep 17 00:00:00 2001 From: Andreas Molzer Date: Sat, 12 Mar 2022 00:21:35 +0100 Subject: Refactor set_ptr_value as with_metadata_of By reversing the arguments we achieve several clarifications: - The function closely resembles `cast` but with an argument to initialized the metadata. This is easier to teach and answers an long outstanding question that had restricted cast to `Sized` targets initially. See multiples reviews of - The 'object identity', in the form or provenance, is now preserved from the call receiver to the result. This helps explain the method as a builder-style, instead of some kind of setter that would modify something in-place. Ensuring that the result has the identity of the `self` argument is also beneficial for an intuition of effects. - An outstanding concern, 'Correct argument type', is avoided by not committing to any specific argument type. This is consistent with cast which does not require its receiver to be a raw address. --- library/alloc/src/rc.rs | 6 +++--- library/alloc/src/sync.rs | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'library/alloc') diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 3065169e5e2..881472c925d 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -895,7 +895,7 @@ impl Rc { // Reverse the offset to find the original RcBox. let rc_ptr = - unsafe { (ptr as *mut RcBox).set_ptr_value((ptr as *mut u8).offset(-offset)) }; + unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut RcBox) }; unsafe { Self::from_ptr(rc_ptr) } } @@ -1338,7 +1338,7 @@ impl Rc { Self::allocate_for_layout( Layout::for_value(&*ptr), |layout| Global.allocate(layout), - |mem| (ptr as *mut RcBox).set_ptr_value(mem), + |mem| mem.with_metadata_of(ptr as *mut RcBox), ) } } @@ -2263,7 +2263,7 @@ impl Weak { let offset = unsafe { data_offset(ptr) }; // Thus, we reverse the offset to get the whole RcBox. // SAFETY: the pointer originated from a Weak, so this offset is safe. - unsafe { (ptr as *mut RcBox).set_ptr_value((ptr as *mut u8).offset(-offset)) } + unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut RcBox) } }; // SAFETY: we now have recovered the original Weak pointer, so can create the Weak. diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 2140c3f168d..e302f874bfd 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -895,7 +895,8 @@ impl Arc { let offset = data_offset(ptr); // Reverse the offset to find the original ArcInner. - let arc_ptr = (ptr as *mut ArcInner).set_ptr_value((ptr as *mut u8).offset(-offset)); + let arc_ptr = + (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut ArcInner); Self::from_ptr(arc_ptr) } @@ -1182,7 +1183,7 @@ impl Arc { Self::allocate_for_layout( Layout::for_value(&*ptr), |layout| Global.allocate(layout), - |mem| (ptr as *mut ArcInner).set_ptr_value(mem) as *mut ArcInner, + |mem| mem.with_metadata_of(ptr as *mut ArcInner), ) } } @@ -1887,7 +1888,7 @@ impl Weak { let offset = unsafe { data_offset(ptr) }; // Thus, we reverse the offset to get the whole RcBox. // SAFETY: the pointer originated from a Weak, so this offset is safe. - unsafe { (ptr as *mut ArcInner).set_ptr_value((ptr as *mut u8).offset(-offset)) } + unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut ArcInner) } }; // SAFETY: we now have recovered the original Weak pointer, so can create the Weak. -- cgit 1.4.1-3-g733a5