Skip to content
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
11 changes: 6 additions & 5 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3780,19 +3780,20 @@ impl<T, A: Allocator> Vec<T, A> {
// they have no further optimizations to apply
#[cfg(not(no_global_oom_handling))]
#[track_caller]
fn extend_desugared<I: Iterator<Item = T>>(&mut self, mut iterator: I) {
fn extend_desugared<I: Iterator<Item = T>>(&mut self, iterator: I) {
// This is the case for a general iterator.
//
// This function should be the moral equivalent of:
//
// for item in iterator {
// self.push(item);
// }
while let Some(element) = iterator.next() {
let (lower, _) = iterator.size_hint();
let initial_len = self.len();
iterator.for_each(move |element| {
let len = self.len();
if len == self.capacity() {
let (lower, _) = iterator.size_hint();
self.reserve(lower.saturating_add(1));
self.reserve(lower.saturating_sub(len - initial_len).saturating_add(1));
}
unsafe {
ptr::write(self.as_mut_ptr().add(len), element);
Expand All @@ -3801,7 +3802,7 @@ impl<T, A: Allocator> Vec<T, A> {
// NB can't overflow since we would have had to alloc the address space
self.set_len(len + 1);
}
}
});
}

// specific extend for `TrustedLen` iterators, called both by the specializations
Expand Down
Loading