Skip to content

Commit 7a7142c

Browse files
committed
Implelement the clone part of Rc::make_mut using Rc::clone_from_ref_in
1 parent cfb859e commit 7a7142c

File tree

1 file changed

+1
-16
lines changed

1 file changed

+1
-16
lines changed

library/alloc/src/rc.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,22 +2067,7 @@ impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Rc<T, A> {
20672067

20682068
if Rc::strong_count(this) != 1 {
20692069
// Gotta clone the data, there are other Rcs.
2070-
2071-
let this_data_ref: &T = &**this;
2072-
// `in_progress` drops the allocation if we panic before finishing initializing it.
2073-
let mut in_progress: UniqueRcUninit<T, A> =
2074-
UniqueRcUninit::new(this_data_ref, this.alloc.clone());
2075-
2076-
// Initialize with clone of this.
2077-
let initialized_clone = unsafe {
2078-
// Clone. If the clone panics, `in_progress` will be dropped and clean up.
2079-
this_data_ref.clone_to_uninit(in_progress.data_ptr().cast());
2080-
// Cast type of pointer, now that it is initialized.
2081-
in_progress.into_rc()
2082-
};
2083-
2084-
// Replace `this` with newly constructed Rc.
2085-
*this = initialized_clone;
2070+
*this = Rc::clone_from_ref_in(&**this, this.alloc.clone());
20862071
} else if Rc::weak_count(this) != 0 {
20872072
// Can just steal the data, all that's left is Weaks
20882073

0 commit comments

Comments
 (0)