Skip to content

Commit a34f11c

Browse files
committed
vec: Use SpecCloneIntoVec::clone_into to implement Vec::clone_from
In the past, Vec::clone_from was implemented using slice::clone_into. The code from clone_into was later duplicated into clone_from in 8725e4c, which is the commit that adds custom allocator support to Vec. Presumably this was done because the slice::clone_into only works for vecs with the default allocator so it would have the wrong type to clone into Vec<T, A>. Now that the clone_into implementation is moved out into a specializable trait anyway we might as well use that to share the code between the two methods.
1 parent ba80c66 commit a34f11c

File tree

1 file changed

+1
-30
lines changed

1 file changed

+1
-30
lines changed

library/alloc/src/vec/mod.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -2646,35 +2646,6 @@ impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
26462646
}
26472647
}
26482648

2649-
#[cfg(not(no_global_oom_handling))]
2650-
trait SpecCloneFrom {
2651-
fn clone_from(this: &mut Self, other: &Self);
2652-
}
2653-
2654-
#[cfg(not(no_global_oom_handling))]
2655-
impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> {
2656-
default fn clone_from(this: &mut Self, other: &Self) {
2657-
// drop anything that will not be overwritten
2658-
this.truncate(other.len());
2659-
2660-
// self.len <= other.len due to the truncate above, so the
2661-
// slices here are always in-bounds.
2662-
let (init, tail) = other.split_at(this.len());
2663-
2664-
// reuse the contained values' allocations/resources.
2665-
this.clone_from_slice(init);
2666-
this.extend_from_slice(tail);
2667-
}
2668-
}
2669-
2670-
#[cfg(not(no_global_oom_handling))]
2671-
impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> {
2672-
fn clone_from(this: &mut Self, other: &Self) {
2673-
this.clear();
2674-
this.extend_from_slice(other);
2675-
}
2676-
}
2677-
26782649
#[cfg(not(no_global_oom_handling))]
26792650
#[stable(feature = "rust1", since = "1.0.0")]
26802651
impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {
@@ -2695,7 +2666,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {
26952666
}
26962667

26972668
fn clone_from(&mut self, other: &Self) {
2698-
SpecCloneFrom::clone_from(self, other)
2669+
crate::slice::SpecCloneIntoVec::clone_into(other.as_slice(), self);
26992670
}
27002671
}
27012672

0 commit comments

Comments
 (0)