From 5d81f76712ea2b9a06d3814ec3d553b51ceedbbe Mon Sep 17 00:00:00 2001 From: Frank Steffahn Date: Thu, 3 Dec 2020 13:25:43 +0100 Subject: [PATCH] Add SAFETY comment. --- library/alloc/src/sync.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index dc8682e890057..508ad630bcb6c 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -623,11 +623,11 @@ impl Arc { /// ``` #[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 { - // 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; } @@ -637,7 +637,13 @@ impl Arc { // 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 });