Skip to content

Commit

Permalink
Match VecDeque::extend to Vec::extend
Browse files Browse the repository at this point in the history
  • Loading branch information
crgl committed Nov 13, 2019
1 parent 5dda3ee commit 164d1a2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,22 @@ impl<'a, T> IntoIterator for &'a mut VecDeque<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> Extend<A> for VecDeque<A> {
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
iter.into_iter().for_each(move |elt| self.push_back(elt));
// This function should be the moral equivalent of:
//
// for item in iter.into_iter() {
// self.push_back(item);
// }
let mut iter = iter.into_iter();
while let Some(element) = iter.next() {
if self.len() == self.capacity() {
let (lower, _) = iter.size_hint();
self.reserve(lower.saturating_add(1));
}

let head = self.head;
self.head = self.wrap_add(self.head, 1);
unsafe { self.buffer_write(head, element); }
}
}
}

Expand Down

0 comments on commit 164d1a2

Please sign in to comment.