1 // SPDX-License-Identifier: Apache-2.0 OR MIT
3 use crate::alloc::Allocator;
4 use crate::collections::{TryReserveError, TryReserveErrorKind};
5 use core::iter::TrustedLen;
7 use core::slice::{self};
9 use super::{IntoIter, SetLenOnDrop, Vec};
11 // Specialization trait used for Vec::extend
12 #[cfg(not(no_global_oom_handling))]
13 pub(super) trait SpecExtend<T, I> {
14 fn spec_extend(&mut self, iter: I);
17 // Specialization trait used for Vec::try_extend
18 pub(super) trait TrySpecExtend<T, I> {
19 fn try_spec_extend(&mut self, iter: I) -> Result<(), TryReserveError>;
22 #[cfg(not(no_global_oom_handling))]
23 impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
25 I: Iterator<Item = T>,
27 default fn spec_extend(&mut self, iter: I) {
28 self.extend_desugared(iter)
32 impl<T, I, A: Allocator> TrySpecExtend<T, I> for Vec<T, A>
34 I: Iterator<Item = T>,
36 default fn try_spec_extend(&mut self, iter: I) -> Result<(), TryReserveError> {
37 self.try_extend_desugared(iter)
41 #[cfg(not(no_global_oom_handling))]
42 impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
44 I: TrustedLen<Item = T>,
46 default fn spec_extend(&mut self, iterator: I) {
47 // This is the case for a TrustedLen iterator.
48 let (low, high) = iterator.size_hint();
49 if let Some(additional) = high {
53 "TrustedLen iterator's size hint is not exact: {:?}",
56 self.reserve(additional);
58 let mut ptr = self.as_mut_ptr().add(self.len());
59 let mut local_len = SetLenOnDrop::new(&mut self.len);
60 iterator.for_each(move |element| {
61 ptr::write(ptr, element);
63 // Since the loop executes user code which can panic we have to bump the pointer
65 // NB can't overflow since we would have had to alloc the address space
66 local_len.increment_len(1);
70 // Per TrustedLen contract a `None` upper bound means that the iterator length
71 // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway.
72 // Since the other branch already panics eagerly (via `reserve()`) we do the same here.
73 // This avoids additional codegen for a fallback code path which would eventually
75 panic!("capacity overflow");
80 impl<T, I, A: Allocator> TrySpecExtend<T, I> for Vec<T, A>
82 I: TrustedLen<Item = T>,
84 default fn try_spec_extend(&mut self, iterator: I) -> Result<(), TryReserveError> {
85 // This is the case for a TrustedLen iterator.
86 let (low, high) = iterator.size_hint();
87 if let Some(additional) = high {
91 "TrustedLen iterator's size hint is not exact: {:?}",
94 self.try_reserve(additional)?;
96 let mut ptr = self.as_mut_ptr().add(self.len());
97 let mut local_len = SetLenOnDrop::new(&mut self.len);
98 iterator.for_each(move |element| {
99 ptr::write(ptr, element);
101 // Since the loop executes user code which can panic we have to bump the pointer
103 // NB can't overflow since we would have had to alloc the address space
104 local_len.increment_len(1);
109 Err(TryReserveErrorKind::CapacityOverflow.into())
114 #[cfg(not(no_global_oom_handling))]
115 impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A> {
116 fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
118 self.append_elements(iterator.as_slice() as _);
120 iterator.forget_remaining_elements();
124 impl<T, A: Allocator> TrySpecExtend<T, IntoIter<T>> for Vec<T, A> {
125 fn try_spec_extend(&mut self, mut iterator: IntoIter<T>) -> Result<(), TryReserveError> {
127 self.try_append_elements(iterator.as_slice() as _)?;
129 iterator.forget_remaining_elements();
134 #[cfg(not(no_global_oom_handling))]
135 impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A>
137 I: Iterator<Item = &'a T>,
140 default fn spec_extend(&mut self, iterator: I) {
141 self.spec_extend(iterator.cloned())
145 impl<'a, T: 'a, I, A: Allocator + 'a> TrySpecExtend<&'a T, I> for Vec<T, A>
147 I: Iterator<Item = &'a T>,
150 default fn try_spec_extend(&mut self, iterator: I) -> Result<(), TryReserveError> {
151 self.try_spec_extend(iterator.cloned())
155 #[cfg(not(no_global_oom_handling))]
156 impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
160 fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
161 let slice = iterator.as_slice();
162 unsafe { self.append_elements(slice) };
166 impl<'a, T: 'a, A: Allocator + 'a> TrySpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
170 fn try_spec_extend(&mut self, iterator: slice::Iter<'a, T>) -> Result<(), TryReserveError> {
171 let slice = iterator.as_slice();
172 unsafe { self.try_append_elements(slice) }