Skip to content

Add Arc::into_inner for safely discarding Arcs without calling the destructor on the inner type. #79665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add SAFETY comment.
  • Loading branch information
steffahn committed Dec 3, 2020
commit 5d81f76712ea2b9a06d3814ec3d553b51ceedbbe
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