Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert to extend_from_slice's previous implementation #38174

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,26 @@ 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.extend(other.iter().cloned())
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
}
}
}

Expand Down Expand Up @@ -1669,7 +1688,26 @@ 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) {
self.extend(iter.into_iter().map(|&x| x))
<I as SpecExtendRef<T>>::extend_vec(iter, self);
}
}

// helper trait for specialization of Vec's Extend<&T> impl
trait SpecExtendRef<T> {
fn extend_vec(self, vec: &mut Vec<T>);
}

impl<'a, T: 'a + Copy, I> SpecExtendRef<T> for I
where I: IntoIterator<Item=&'a T>
{
default fn extend_vec(self, vec: &mut Vec<T>) {
vec.extend(self.into_iter().map(|&x| x))
}
}

impl<'a, T: Copy> SpecExtendRef<T> for &'a [T] {
fn extend_vec(self, vec: &mut Vec<T>) {
vec.extend_from_slice(self);
}
}

Expand Down