Skip to content

Commit bf5e5a8

Browse files
Use Box::from_raw instead of ptr::drop_in_place as that actually dealloc's the Box (i'm dumb and misinterpreted the std code :/); fix some desync between code in between sections
1 parent 7ca548e commit bf5e5a8

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/arc-base.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ impl<T> Arc<T> {
2525
data,
2626
});
2727
Arc {
28-
// It is okay to call `.unwrap()` here as we get a pointer from `Box::into_raw` which is guaranteed to not be null.
28+
// It is okay to call `.unwrap()` here as we get a pointer from
29+
// `Box::into_raw` which is guaranteed to not be null.
2930
ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
3031
_marker: PhantomData,
3132
}
@@ -84,8 +85,8 @@ impl<T> Arc<T> {
8485
data,
8586
});
8687
Arc {
87-
// SOUNDNESS: It is sound to call `.unwrap()` here as we get a
88-
// pointer from `Box::into_raw` which is guaranteed to not be null.
88+
// It is okay to call `.unwrap()` here as we get a pointer from
89+
// `Box::into_raw` which is guaranteed to not be null.
8990
ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
9091
_marker: PhantomData,
9192
}

src/arc-drop.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ We'll need to import `std::sync::atomic` itself:
6363
use std::sync::atomic;
6464
```
6565

66-
Finally, we can drop the data itself. We use `std::ptr::drop_in_place` as it is
67-
better than using `std::ptr::read` then dropping that object (see [its
68-
documentation](https://doc.rust-lang.org/stable/std/ptr/fn.drop_in_place.html)).
69-
This takes a `*mut T` and not a `NonNull<T>`, so we must convert using
70-
`NonNull::as_ptr`.
66+
Finally, we can drop the data itself. We use `Box::from_raw` to drop the boxed
67+
`ArcInner<T>` and its data. This takes a `*mut T` and not a `NonNull<T>`, so we
68+
must convert using `NonNull::as_ptr`.
7169

7270
```rust,ignore
73-
unsafe { std::ptr::drop_in_place(self.ptr.as_ptr()) }
71+
unsafe { stdBox::from_raw(self.ptr.as_ptr()) }
7472
```
7573

7674
This is safe as we know we have the last pointer to the `ArcInner` and that its
@@ -88,7 +86,7 @@ impl<T> Drop for Arc<T> {
8886
atomic::fence(Ordering::Acquire);
8987
// This is safe as we know we have the last pointer to the `ArcInner`
9088
// and that its pointer is valid.
91-
unsafe { std::ptr::drop_in_place(self.ptr.as_ptr()) }
89+
unsafe { Box::from_raw(self.ptr.as_ptr()) }
9290
}
9391
}
9492
```

src/arc-final.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ impl<T> Arc<T> {
2626
data,
2727
});
2828
Arc {
29-
// SOUNDNESS: It is sound to call `.unwrap()` here as we get a
30-
// pointer from `Box::into_raw` which is guaranteed to not be null.
29+
// It is okay to call `.unwrap()` here as we get a pointer from
30+
// `Box::into_raw` which is guaranteed to not be null.
3131
ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
3232
_marker: PhantomData,
3333
}
3434
}
3535

3636
fn inner(&self) -> &ArcInner<T> {
3737
// This unsafety is okay because while this Arc is alive, we're
38-
// guaranteed that the inner pointer is valid.
38+
// guaranteed that the inner pointer is valid. Also, ArcInner<T> is
39+
// Sync if T is Sync.
3940
unsafe { self.ptr.as_ref() }
4041
}
4142
}
@@ -65,7 +66,7 @@ impl<T> Drop for Arc<T> {
6566
atomic::fence(Ordering::Acquire);
6667
// This is safe as we know we have the last pointer to the `ArcInner`
6768
// and that its pointer is valid.
68-
unsafe { std::ptr::drop_in_place(self.ptr.as_ptr()) }
69+
unsafe { Box::from_raw(self.ptr.as_ptr()) }
6970
}
7071
}
7172

0 commit comments

Comments
 (0)