Skip to content

Commit

Permalink
Add SAFETY comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
steffahn committed Dec 3, 2020
1 parent a7bdb90 commit 5d81f76
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,11 @@ impl<T> Arc<T> {
/// ```
#[inline]
#[unstable(feature = "unwrap_or_drop", issue = "none")] // FIXME: add issue
// FIXME: should this copy all/some of the comments from drop and drop_slow?
pub fn unwrap_or_drop(this: Self) -> Option<T> {
// following the implementation of `drop` (and `drop_slow`)
// Make sure that the ordinary `Drop` implementation isn’t called as well
let mut this = core::mem::ManuallyDrop::new(this);

// Following the implementation of `drop` and `drop_slow`
if this.inner().strong.fetch_sub(1, Release) != 1 {
return None;
}
Expand All @@ -637,7 +637,13 @@ impl<T> Arc<T> {
// FIXME: should the part below this be moved into a seperate #[inline(never)]
// function, like it's done with drop_slow in drop?

// using `ptr::read` where `drop_slow` was using `ptr::drop_in_place`
// SAFETY: This mirrors the line
//
// unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) };
//
// in `drop_slow`. Instead of dropping the value behind the pointer
// it is read and eventually returned; `ptr::read` has the same
// safety conditions as `ptr::drop_in_place`.
let inner = unsafe { ptr::read(Self::get_mut_unchecked(&mut this)) };

drop(Weak { ptr: this.ptr });
Expand Down

0 comments on commit 5d81f76

Please sign in to comment.