Skip to content

Commit

Permalink
Specialize Vec::extend to Vec::extend_from_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
fhartwig committed Oct 11, 2016
1 parent e335620 commit 63ee8d0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,24 @@ 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().cloned());
<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);
}
}

Expand Down

0 comments on commit 63ee8d0

Please sign in to comment.