Skip to content

Commit 2eac4db

Browse files
committed
Implement the clone part of Arc::make_mut using Arc::clone_from_ref_in
1 parent b565635 commit 2eac4db

File tree

1 file changed

+1
-13
lines changed

1 file changed

+1
-13
lines changed

library/alloc/src/sync.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,19 +2502,7 @@ impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Arc<T, A> {
25022502
// deallocated.
25032503
if this.inner().strong.compare_exchange(1, 0, Acquire, Relaxed).is_err() {
25042504
// Another strong pointer exists, so we must clone.
2505-
2506-
let this_data_ref: &T = &**this;
2507-
// `in_progress` drops the allocation if we panic before finishing initializing it.
2508-
let mut in_progress: UniqueArcUninit<T, A> =
2509-
UniqueArcUninit::new(this_data_ref, this.alloc.clone());
2510-
2511-
let initialized_clone = unsafe {
2512-
// Clone. If the clone panics, `in_progress` will be dropped and clean up.
2513-
this_data_ref.clone_to_uninit(in_progress.data_ptr().cast());
2514-
// Cast type of pointer, now that it is initialized.
2515-
in_progress.into_arc()
2516-
};
2517-
*this = initialized_clone;
2505+
*this = Arc::clone_from_ref_in(&**this, this.alloc.clone());
25182506
} else if this.inner().weak.load(Relaxed) != 1 {
25192507
// Relaxed suffices in the above because this is fundamentally an
25202508
// optimization: we are always racing with weak pointers being

0 commit comments

Comments
 (0)