diff options
| author | CAD97 <cad97@cad97.com> | 2020-07-08 16:23:38 -0400 |
|---|---|---|
| committer | CAD97 <cad97@cad97.com> | 2020-09-12 10:27:43 -0500 |
| commit | 728938346b02a9688c44253c19b15baa7551fd80 (patch) | |
| tree | 46141239cf3e71bd42ac0aac507216ab83ec59f1 /library/alloc/src | |
| parent | 85109afee9d12153849a68abe3b982268c188070 (diff) | |
Adjust rc::Weak::from_raw to support unsized T
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/rc.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index f998e49dcfc..27e6e110903 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1841,17 +1841,18 @@ impl<T> Weak<T> { /// [`new`]: Weak::new #[stable(feature = "weak_into_raw", since = "1.45.0")] pub unsafe fn from_raw(ptr: *const T) -> Self { - if ptr.is_null() { - Self::new() - } else { - // See Rc::from_raw for details - unsafe { - let offset = data_offset(ptr); - let fake_ptr = ptr as *mut RcBox<T>; - let ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset)); - Weak { ptr: NonNull::new(ptr).expect("Invalid pointer passed to from_raw") } - } - } + // SAFETY: data_offset is safe to call, because this pointer originates from a Weak. + // See Weak::as_ptr for context on how the input pointer is derived. + let offset = unsafe { data_offset(ptr) }; + + // Reverse the offset to find the original RcBox. + // SAFETY: we use wrapping_offset here because the pointer may be dangling (iff T: Sized). + let ptr = unsafe { + set_data_ptr(ptr as *mut RcBox<T>, (ptr as *mut u8).wrapping_offset(-offset)) + }; + + // SAFETY: we now have recovered the original Weak pointer, so can create the Weak. + Weak { ptr: unsafe { NonNull::new_unchecked(ptr) } } } } |
