Skip to content

Commit 2cff12e

Browse files
committed
Small refactoring to make this code more clear
This hairy conditional doesn't need to be so. It _does_ need to be a thin pointer, otherwise, it will fail to compile, so let's pull that out into a temporary for future readers of the source. Also, after a discussion with @pnkfelix and @gankro, we don't need these null checks anymore, as zero-on-drop has been gone for a while now.
1 parent 27a1834 commit 2cff12e

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/liballoc/arc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,9 @@ impl<T: ?Sized> Drop for Arc<T> {
554554
// This structure has #[unsafe_no_drop_flag], so this drop glue may run
555555
// more than once (but it is guaranteed to be zeroed after the first if
556556
// it's run more than once)
557-
let ptr = *self._ptr;
558-
// if ptr.is_null() { return }
559-
if ptr as *mut u8 as usize == 0 || ptr as *mut u8 as usize == mem::POST_DROP_USIZE {
557+
let thin = *self._ptr as *const ();
558+
559+
if thin as usize == mem::POST_DROP_USIZE {
560560
return;
561561
}
562562

@@ -709,9 +709,10 @@ impl<T: ?Sized> Drop for Weak<T> {
709709
/// ```
710710
fn drop(&mut self) {
711711
let ptr = *self._ptr;
712+
let thin = ptr as *const ();
712713

713714
// see comments above for why this check is here
714-
if ptr as *mut u8 as usize == 0 || ptr as *mut u8 as usize == mem::POST_DROP_USIZE {
715+
if thin as usize == mem::POST_DROP_USIZE {
715716
return;
716717
}
717718

src/liballoc/rc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,9 @@ impl<T: ?Sized> Drop for Rc<T> {
449449
fn drop(&mut self) {
450450
unsafe {
451451
let ptr = *self._ptr;
452-
if !(*(&ptr as *const _ as *const *const ())).is_null() &&
453-
ptr as *const () as usize != mem::POST_DROP_USIZE {
452+
let thin = ptr as *const ();
453+
454+
if thin as usize != mem::POST_DROP_USIZE {
454455
self.dec_strong();
455456
if self.strong() == 0 {
456457
// destroy the contained object
@@ -782,8 +783,9 @@ impl<T: ?Sized> Drop for Weak<T> {
782783
fn drop(&mut self) {
783784
unsafe {
784785
let ptr = *self._ptr;
785-
if !(*(&ptr as *const _ as *const *const ())).is_null() &&
786-
ptr as *const () as usize != mem::POST_DROP_USIZE {
786+
let thin = ptr as *const ();
787+
788+
if thin as usize != mem::POST_DROP_USIZE {
787789
self.dec_weak();
788790
// the weak count starts at 1, and will only go to zero if all
789791
// the strong pointers have disappeared.

0 commit comments

Comments
 (0)