This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +12
-11
lines changed Expand file tree Collapse file tree 1 file changed +12
-11
lines changed Original file line number Diff line number Diff line change @@ -1629,17 +1629,18 @@ impl<T> Weak<T> {
1629
1629
/// [`forget`]: std::mem::forget
1630
1630
#[ stable( feature = "weak_into_raw" , since = "1.45.0" ) ]
1631
1631
pub unsafe fn from_raw ( ptr : * const T ) -> Self {
1632
- if ptr. is_null ( ) {
1633
- Self :: new ( )
1634
- } else {
1635
- // See Arc::from_raw for details
1636
- unsafe {
1637
- let offset = data_offset ( ptr) ;
1638
- let fake_ptr = ptr as * mut ArcInner < T > ;
1639
- let ptr = set_data_ptr ( fake_ptr, ( ptr as * mut u8 ) . offset ( -offset) ) ;
1640
- Weak { ptr : NonNull :: new ( ptr) . expect ( "Invalid pointer passed to from_raw" ) }
1641
- }
1642
- }
1632
+ // SAFETY: data_offset is safe to call, because this pointer originates from a Weak.
1633
+ // See Weak::as_ptr for context on how the input pointer is derived.
1634
+ let offset = unsafe { data_offset ( ptr) } ;
1635
+
1636
+ // Reverse the offset to find the original ArcInner.
1637
+ // SAFETY: we use wrapping_offset here because the pointer may be dangling (iff T: Sized)
1638
+ let ptr = unsafe {
1639
+ set_data_ptr ( ptr as * mut ArcInner < T > , ( ptr as * mut u8 ) . wrapping_offset ( -offset) )
1640
+ } ;
1641
+
1642
+ // SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
1643
+ unsafe { Weak { ptr : NonNull :: new_unchecked ( ptr) } }
1643
1644
}
1644
1645
}
1645
1646
You can’t perform that action at this time.
0 commit comments