Skip to content

Commit 622f24f

Browse files
committed
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.
1 parent a3cab90 commit 622f24f

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed

src/libcollections/vec.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,26 +1212,7 @@ impl<T: Clone> Vec<T> {
12121212
/// ```
12131213
#[stable(feature = "vec_extend_from_slice", since = "1.6.0")]
12141214
pub fn extend_from_slice(&mut self, other: &[T]) {
1215-
self.reserve(other.len());
1216-
1217-
// Unsafe code so this can be optimised to a memcpy (or something
1218-
// similarly fast) when T is Copy. LLVM is easily confused, so any
1219-
// extra operations during the loop can prevent this optimisation.
1220-
unsafe {
1221-
let len = self.len();
1222-
let ptr = self.get_unchecked_mut(len) as *mut T;
1223-
// Use SetLenOnDrop to work around bug where compiler
1224-
// may not realize the store through `ptr` trough self.set_len()
1225-
// don't alias.
1226-
let mut local_len = SetLenOnDrop::new(&mut self.len);
1227-
1228-
for i in 0..other.len() {
1229-
ptr::write(ptr.offset(i as isize), other.get_unchecked(i).clone());
1230-
local_len.increment_len(1);
1231-
}
1232-
1233-
// len set by scope guard
1234-
}
1215+
self.extend(other.iter().cloned())
12351216
}
12361217
}
12371218

@@ -1640,24 +1621,7 @@ impl<T> Vec<T> {
16401621
#[stable(feature = "extend_ref", since = "1.2.0")]
16411622
impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
16421623
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
1643-
<I as SpecExtendVec<T>>::extend_vec(iter, self);
1644-
}
1645-
}
1646-
1647-
// helper trait for specialization of Vec's Extend impl
1648-
trait SpecExtendVec<T> {
1649-
fn extend_vec(self, vec: &mut Vec<T>);
1650-
}
1651-
1652-
impl <'a, T: 'a + Copy, I: IntoIterator<Item=&'a T>> SpecExtendVec<T> for I {
1653-
default fn extend_vec(self, vec: &mut Vec<T>) {
1654-
vec.extend(self.into_iter().cloned());
1655-
}
1656-
}
1657-
1658-
impl<'a, T: Copy> SpecExtendVec<T> for &'a [T] {
1659-
fn extend_vec(self, vec: &mut Vec<T>) {
1660-
vec.extend_from_slice(self);
1624+
self.extend(iter.into_iter().map(|&x| x))
16611625
}
16621626
}
16631627

0 commit comments

Comments
 (0)