From 622f24f6d9cc952ccf2bb4dbe5bf65bb966194d3 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Fri, 21 Oct 2016 13:51:31 +0200 Subject: [PATCH] vec: Use Vec::extend specializations in extend_from_slice and more The new Vec::extend covers the duties of .extend_from_slice() and some previous specializations. --- src/libcollections/vec.rs | 40 ++------------------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index cd628a39af8b..50ad48567472 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1212,26 +1212,7 @@ impl Vec { /// ``` #[stable(feature = "vec_extend_from_slice", since = "1.6.0")] pub fn extend_from_slice(&mut self, other: &[T]) { - self.reserve(other.len()); - - // Unsafe code so this can be optimised to a memcpy (or something - // similarly fast) when T is Copy. LLVM is easily confused, so any - // extra operations during the loop can prevent this optimisation. - unsafe { - let len = self.len(); - let ptr = self.get_unchecked_mut(len) as *mut T; - // Use SetLenOnDrop to work around bug where compiler - // may not realize the store through `ptr` trough self.set_len() - // don't alias. - let mut local_len = SetLenOnDrop::new(&mut self.len); - - for i in 0..other.len() { - ptr::write(ptr.offset(i as isize), other.get_unchecked(i).clone()); - local_len.increment_len(1); - } - - // len set by scope guard - } + self.extend(other.iter().cloned()) } } @@ -1640,24 +1621,7 @@ impl Vec { #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a, T: 'a + Copy> Extend<&'a T> for Vec { fn extend>(&mut self, iter: I) { - >::extend_vec(iter, self); - } -} - -// helper trait for specialization of Vec's Extend impl -trait SpecExtendVec { - fn extend_vec(self, vec: &mut Vec); -} - -impl <'a, T: 'a + Copy, I: IntoIterator> SpecExtendVec for I { - default fn extend_vec(self, vec: &mut Vec) { - vec.extend(self.into_iter().cloned()); - } -} - -impl<'a, T: Copy> SpecExtendVec for &'a [T] { - fn extend_vec(self, vec: &mut Vec) { - vec.extend_from_slice(self); + self.extend(iter.into_iter().map(|&x| x)) } }