diff options
| author | Ralf Jung <post@ralfj.de> | 2022-11-27 20:51:52 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2022-11-27 20:51:52 +0100 |
| commit | 7c12ed1d5dc6059972b06fb7257a239f3a33ec88 (patch) | |
| tree | fd57c555e715f92ec17d90a702860a6740d32ce8 /library/alloc/src/vec/spec_extend.rs | |
| parent | 187ba6778174717b9d793aec363e836c947b55a5 (diff) | |
| parent | 454784afba5bf35b5ff14ada0e31265ad1d75e73 (diff) | |
Merge from rustc
Diffstat (limited to 'library/alloc/src/vec/spec_extend.rs')
| -rw-r--r-- | library/alloc/src/vec/spec_extend.rs | 34 |
1 files changed, 2 insertions, 32 deletions
diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index 1ea9c827afd..56065ce565b 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -1,9 +1,8 @@ use crate::alloc::Allocator; use core::iter::TrustedLen; -use core::ptr::{self}; use core::slice::{self}; -use super::{IntoIter, SetLenOnDrop, Vec}; +use super::{IntoIter, Vec}; // Specialization trait used for Vec::extend pub(super) trait SpecExtend<T, I> { @@ -24,36 +23,7 @@ where I: TrustedLen<Item = T>, { default fn spec_extend(&mut self, iterator: I) { - // This is the case for a TrustedLen iterator. - let (low, high) = iterator.size_hint(); - if let Some(additional) = high { - debug_assert_eq!( - low, - additional, - "TrustedLen iterator's size hint is not exact: {:?}", - (low, high) - ); - self.reserve(additional); - unsafe { - let mut ptr = self.as_mut_ptr().add(self.len()); - 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. - // NB can't overflow since we would have had to alloc the address space - local_len.increment_len(1); - }); - } - } else { - // Per TrustedLen contract a `None` upper bound means that the iterator length - // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway. - // Since the other branch already panics eagerly (via `reserve()`) we do the same here. - // This avoids additional codegen for a fallback code path which would eventually - // panic anyway. - panic!("capacity overflow"); - } + self.extend_trusted(iterator) } } |
