diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index f2632412700..c9f9e513ef3 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1244,7 +1244,7 @@ impl Vec { /// ``` #[stable(feature = "vec_extend_from_slice", since = "1.6.0")] pub fn extend_from_slice(&mut self, other: &[T]) { - self.extend(other.iter().cloned()) + self.spec_extend(other.iter()) } } @@ -1499,7 +1499,7 @@ impl ops::DerefMut for Vec { impl FromIterator for Vec { #[inline] fn from_iter>(iter: I) -> Vec { - >::from_iter(iter.into_iter()) + >::from_iter(iter.into_iter()) } } @@ -1572,12 +1572,12 @@ impl Extend for Vec { } // Specialization trait used for Vec::from_iter and Vec::extend -trait SpecExtend { +trait SpecExtend { fn from_iter(iter: I) -> Self; fn spec_extend(&mut self, iter: I); } -impl SpecExtend for Vec +impl SpecExtend for Vec where I: Iterator, { default fn from_iter(mut iterator: I) -> Self { @@ -1607,7 +1607,7 @@ impl SpecExtend for Vec } } -impl SpecExtend for Vec +impl SpecExtend for Vec where I: TrustedLen, { fn from_iter(iterator: I) -> Self { @@ -1642,6 +1642,33 @@ impl SpecExtend for Vec } } +impl<'a, T: 'a, I> SpecExtend<&'a T, I> for Vec + where I: Iterator, + T: Clone, +{ + default fn from_iter(iterator: I) -> Self { + SpecExtend::from_iter(iterator.cloned()) + } + + default fn spec_extend(&mut self, iterator: I) { + self.spec_extend(iterator.cloned()) + } +} + +impl<'a, T: 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec + where T: Copy, +{ + fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) { + let slice = iterator.as_slice(); + self.reserve(slice.len()); + unsafe { + let len = self.len(); + self.set_len(len + slice.len()); + self.get_unchecked_mut(len..).copy_from_slice(slice); + } + } +} + impl Vec { fn extend_desugared>(&mut self, mut iterator: I) { // This is the case for a general iterator. @@ -1669,7 +1696,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) { - self.extend(iter.into_iter().map(|&x| x)) + self.spec_extend(iter.into_iter()) } }