Skip to content

Commit

Permalink
Simplify Weak::as_ptr impl
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jul 1, 2020
1 parent ac40d1e commit 98789ac
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,6 @@ impl<T> Weak<T> {
#[stable(feature = "weak_into_raw", since = "1.45.0")]
pub fn as_ptr(&self) -> *const T {
let ptr: *mut RcBox<T> = NonNull::as_ptr(self.ptr);
let fake_ptr = ptr as *mut T;

// SAFETY: we must offset the pointer manually, and said pointer may be
// a dangling weak (usize::MAX). data_offset is safe to call, because we
Expand All @@ -1712,8 +1711,8 @@ impl<T> Weak<T> {
// is used so that we can use the same code path for the non-dangling
// unsized case and the potentially dangling sized case.
unsafe {
let offset = data_offset(fake_ptr);
set_data_ptr(fake_ptr, (ptr as *mut u8).wrapping_offset(offset))
let offset = data_offset(ptr as *mut T);
set_data_ptr(ptr as *mut T, (ptr as *mut u8).wrapping_offset(offset))
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,6 @@ impl<T> Weak<T> {
#[stable(feature = "weak_into_raw", since = "1.45.0")]
pub fn as_ptr(&self) -> *const T {
let ptr: *mut ArcInner<T> = NonNull::as_ptr(self.ptr);
let fake_ptr = ptr as *mut T;

// SAFETY: we must offset the pointer manually, and said pointer may be
// a dangling weak (usize::MAX). data_offset is safe to call, because we
Expand All @@ -1480,8 +1479,8 @@ impl<T> Weak<T> {
// is used so that we can use the same code path for the non-dangling
// unsized case and the potentially dangling sized case.
unsafe {
let offset = data_offset(fake_ptr);
set_data_ptr(fake_ptr, (ptr as *mut u8).wrapping_offset(offset))
let offset = data_offset(ptr as *mut T);
set_data_ptr(ptr as *mut T, (ptr as *mut u8).wrapping_offset(offset))
}
}

Expand Down

0 comments on commit 98789ac

Please sign in to comment.