diff options
| author | Neil Roberts <bpeeluk@yahoo.co.uk> | 2023-01-28 14:52:31 +0100 |
|---|---|---|
| committer | Neil Roberts <bpeeluk@yahoo.co.uk> | 2023-01-28 20:37:01 +0100 |
| commit | a34f11c006936f0935c0f3484f9a292901d0eade (patch) | |
| tree | 05f4f78b355e27cbec00b0fdb00a33ec36ba7c23 /library/alloc/src/vec | |
| parent | ba80c662f448c69cbd184d18630b8824f0169b2e (diff) | |
| download | rust-a34f11c006936f0935c0f3484f9a292901d0eade.tar.gz rust-a34f11c006936f0935c0f3484f9a292901d0eade.zip | |
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 8725e4c337, 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.
Diffstat (limited to 'library/alloc/src/vec')
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 31 |
1 files changed, 1 insertions, 30 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 36b0b3c9e7c..a07f3da78d3 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2647,35 +2647,6 @@ impl<T, A: Allocator> ops::DerefMut for Vec<T, A> { } #[cfg(not(no_global_oom_handling))] -trait SpecCloneFrom { - fn clone_from(this: &mut Self, other: &Self); -} - -#[cfg(not(no_global_oom_handling))] -impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> { - default fn clone_from(this: &mut Self, other: &Self) { - // drop anything that will not be overwritten - this.truncate(other.len()); - - // self.len <= other.len due to the truncate above, so the - // slices here are always in-bounds. - let (init, tail) = other.split_at(this.len()); - - // reuse the contained values' allocations/resources. - this.clone_from_slice(init); - this.extend_from_slice(tail); - } -} - -#[cfg(not(no_global_oom_handling))] -impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> { - fn clone_from(this: &mut Self, other: &Self) { - this.clear(); - this.extend_from_slice(other); - } -} - -#[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> { #[cfg(not(test))] @@ -2695,7 +2666,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> { } fn clone_from(&mut self, other: &Self) { - SpecCloneFrom::clone_from(self, other) + crate::slice::SpecCloneIntoVec::clone_into(other.as_slice(), self); } } |
