diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2022-11-24 19:14:19 -0800 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2022-11-24 19:14:19 -0800 |
| commit | 9d68a1a74c65245c9ae7b5db2c552c995697e8ef (patch) | |
| tree | ba257e6f512820fb89ae725ef0b8312af36ac43d /library/alloc/src/vec/mod.rs | |
| parent | a8954f1f6a2245794ff41db9b1cba33a76e7d5f9 (diff) | |
| download | rust-9d68a1a74c65245c9ae7b5db2c552c995697e8ef.tar.gz rust-9d68a1a74c65245c9ae7b5db2c552c995697e8ef.zip | |
Tune RepeatWith::try_fold and Take::for_each and Vec::extend_trusted
Diffstat (limited to 'library/alloc/src/vec/mod.rs')
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 94ebcb863a4..e147af2ce39 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2874,13 +2874,12 @@ impl<T, A: Allocator> Vec<T, A> { ); self.reserve(additional); unsafe { - let mut ptr = self.as_mut_ptr().add(self.len()); + let ptr = self.as_mut_ptr(); let mut local_len = SetLenOnDrop::new(&mut self.len); iterator.for_each(move |element| { - ptr::write(ptr, element); - ptr = ptr.add(1); - // Since the loop executes user code which can panic we have to bump the pointer - // after each step. + ptr::write(ptr.add(local_len.current_len()), element); + // Since the loop executes user code which can panic we have to update + // the length every step to correctly drop what we've written. // NB can't overflow since we would have had to alloc the address space local_len.increment_len(1); }); |
