diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-21 13:51:31 +0200 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-21 14:06:38 +0200 |
| commit | 622f24f6d9cc952ccf2bb4dbe5bf65bb966194d3 (patch) | |
| tree | 6ab820fb97d2393fa9afc216c222274c4a1524ec | |
| parent | a3cab90fda3b1ccfa4eb952dda49677c8d06c1ef (diff) | |
| download | rust-622f24f6d9cc952ccf2bb4dbe5bf65bb966194d3.tar.gz rust-622f24f6d9cc952ccf2bb4dbe5bf65bb966194d3.zip | |
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.
| -rw-r--r-- | src/libcollections/vec.rs | 40 |
1 files changed, 2 insertions, 38 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index cd628a39af8..50ad4856747 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1212,26 +1212,7 @@ impl<T: Clone> Vec<T> { /// ``` #[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<T> Vec<T> { #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> { fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { - <I as SpecExtendVec<T>>::extend_vec(iter, self); - } -} - -// helper trait for specialization of Vec's Extend impl -trait SpecExtendVec<T> { - fn extend_vec(self, vec: &mut Vec<T>); -} - -impl <'a, T: 'a + Copy, I: IntoIterator<Item=&'a T>> SpecExtendVec<T> for I { - default fn extend_vec(self, vec: &mut Vec<T>) { - vec.extend(self.into_iter().cloned()); - } -} - -impl<'a, T: Copy> SpecExtendVec<T> for &'a [T] { - fn extend_vec(self, vec: &mut Vec<T>) { - vec.extend_from_slice(self); + self.extend(iter.into_iter().map(|&x| x)) } } |
