1 // SPDX-License-Identifier: Apache-2.0 OR MIT
3 #[cfg(not(no_global_oom_handling))]
4 use super::AsVecIntoIter;
5 use crate::alloc::{Allocator, Global};
6 use crate::raw_vec::RawVec;
8 use core::intrinsics::arith_offset;
10 FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccessNoCoerce,
12 use core::marker::PhantomData;
13 use core::mem::{self, ManuallyDrop};
14 #[cfg(not(no_global_oom_handling))]
16 use core::ptr::{self, NonNull};
17 use core::slice::{self};
19 /// An iterator that moves out of a vector.
21 /// This `struct` is created by the `into_iter` method on [`Vec`](super::Vec)
22 /// (provided by the [`IntoIterator`] trait).
27 /// let v = vec![0, 1, 2];
28 /// let iter: std::vec::IntoIter<_> = v.into_iter();
30 #[stable(feature = "rust1", since = "1.0.0")]
31 #[rustc_insignificant_dtor]
34 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
36 pub(super) buf: NonNull<T>,
37 pub(super) phantom: PhantomData<T>,
38 pub(super) cap: usize,
39 // the drop impl reconstructs a RawVec from buf, cap and alloc
40 // to avoid dropping the allocator twice we need to wrap it into ManuallyDrop
41 pub(super) alloc: ManuallyDrop<A>,
42 pub(super) ptr: *const T,
43 pub(super) end: *const T,
46 #[stable(feature = "vec_intoiter_debug", since = "1.13.0")]
47 impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
53 impl<T, A: Allocator> IntoIter<T, A> {
54 /// Returns the remaining items of this iterator as a slice.
59 /// let vec = vec!['a', 'b', 'c'];
60 /// let mut into_iter = vec.into_iter();
61 /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
62 /// let _ = into_iter.next().unwrap();
63 /// assert_eq!(into_iter.as_slice(), &['b', 'c']);
65 #[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
66 pub fn as_slice(&self) -> &[T] {
67 unsafe { slice::from_raw_parts(self.ptr, self.len()) }
70 /// Returns the remaining items of this iterator as a mutable slice.
75 /// let vec = vec!['a', 'b', 'c'];
76 /// let mut into_iter = vec.into_iter();
77 /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
78 /// into_iter.as_mut_slice()[2] = 'z';
79 /// assert_eq!(into_iter.next().unwrap(), 'a');
80 /// assert_eq!(into_iter.next().unwrap(), 'b');
81 /// assert_eq!(into_iter.next().unwrap(), 'z');
83 #[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
84 pub fn as_mut_slice(&mut self) -> &mut [T] {
85 unsafe { &mut *self.as_raw_mut_slice() }
88 /// Returns a reference to the underlying allocator.
89 #[unstable(feature = "allocator_api", issue = "32838")]
91 pub fn allocator(&self) -> &A {
95 fn as_raw_mut_slice(&mut self) -> *mut [T] {
96 ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len())
99 /// Drops remaining elements and relinquishes the backing allocation.
101 /// This is roughly equivalent to the following, but more efficient
104 /// # let mut into_iter = Vec::<u8>::with_capacity(10).into_iter();
105 /// (&mut into_iter).for_each(core::mem::drop);
106 /// unsafe { core::ptr::write(&mut into_iter, Vec::new().into_iter()); }
109 /// This method is used by in-place iteration, refer to the vec::in_place_collect
110 /// documentation for an overview.
111 #[cfg(not(no_global_oom_handling))]
112 pub(super) fn forget_allocation_drop_remaining(&mut self) {
113 let remaining = self.as_raw_mut_slice();
115 // overwrite the individual fields instead of creating a new
116 // struct and then overwriting &mut self.
117 // this creates less assembly
119 self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
120 self.ptr = self.buf.as_ptr();
121 self.end = self.buf.as_ptr();
124 ptr::drop_in_place(remaining);
128 /// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed.
130 pub(crate) fn forget_remaining_elements(&mut self) {
135 #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")]
136 impl<T, A: Allocator> AsRef<[T]> for IntoIter<T, A> {
137 fn as_ref(&self) -> &[T] {
142 #[stable(feature = "rust1", since = "1.0.0")]
143 unsafe impl<T: Send, A: Allocator + Send> Send for IntoIter<T, A> {}
144 #[stable(feature = "rust1", since = "1.0.0")]
145 unsafe impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A> {}
147 #[stable(feature = "rust1", since = "1.0.0")]
148 impl<T, A: Allocator> Iterator for IntoIter<T, A> {
152 fn next(&mut self) -> Option<T> {
153 if self.ptr as *const _ == self.end {
155 } else if mem::size_of::<T>() == 0 {
156 // purposefully don't use 'ptr.offset' because for
157 // vectors with 0-size elements this would return the
159 self.ptr = unsafe { arith_offset(self.ptr as *const i8, 1) as *mut T };
161 // Make up a value of this ZST.
162 Some(unsafe { mem::zeroed() })
165 self.ptr = unsafe { self.ptr.offset(1) };
167 Some(unsafe { ptr::read(old) })
172 fn size_hint(&self) -> (usize, Option<usize>) {
173 let exact = if mem::size_of::<T>() == 0 {
174 self.end.addr().wrapping_sub(self.ptr.addr())
176 unsafe { self.end.sub_ptr(self.ptr) }
182 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
183 let step_size = self.len().min(n);
184 let to_drop = ptr::slice_from_raw_parts_mut(self.ptr as *mut T, step_size);
185 if mem::size_of::<T>() == 0 {
186 // SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
187 // effectively results in unsigned pointers representing positions 0..usize::MAX,
188 // which is valid for ZSTs.
189 self.ptr = unsafe { arith_offset(self.ptr as *const i8, step_size as isize) as *mut T }
191 // SAFETY: the min() above ensures that step_size is in bounds
192 self.ptr = unsafe { self.ptr.add(step_size) };
194 // SAFETY: the min() above ensures that step_size is in bounds
196 ptr::drop_in_place(to_drop);
199 return Err(step_size);
205 fn count(self) -> usize {
209 unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> Self::Item
211 Self: TrustedRandomAccessNoCoerce,
213 // SAFETY: the caller must guarantee that `i` is in bounds of the
214 // `Vec<T>`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)`
215 // is guaranteed to pointer to an element of the `Vec<T>` and
216 // thus guaranteed to be valid to dereference.
218 // Also note the implementation of `Self: TrustedRandomAccess` requires
219 // that `T: Copy` so reading elements from the buffer doesn't invalidate
222 if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
227 #[stable(feature = "rust1", since = "1.0.0")]
228 impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
230 fn next_back(&mut self) -> Option<T> {
231 if self.end == self.ptr {
233 } else if mem::size_of::<T>() == 0 {
234 // See above for why 'ptr.offset' isn't used
235 self.end = unsafe { arith_offset(self.end as *const i8, -1) as *mut T };
237 // Make up a value of this ZST.
238 Some(unsafe { mem::zeroed() })
240 self.end = unsafe { self.end.offset(-1) };
242 Some(unsafe { ptr::read(self.end) })
247 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
248 let step_size = self.len().min(n);
249 if mem::size_of::<T>() == 0 {
250 // SAFETY: same as for advance_by()
252 arith_offset(self.end as *const i8, step_size.wrapping_neg() as isize) as *mut T
255 // SAFETY: same as for advance_by()
256 self.end = unsafe { self.end.offset(step_size.wrapping_neg() as isize) };
258 let to_drop = ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size);
259 // SAFETY: same as for advance_by()
261 ptr::drop_in_place(to_drop);
264 return Err(step_size);
270 #[stable(feature = "rust1", since = "1.0.0")]
271 impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
272 fn is_empty(&self) -> bool {
277 #[stable(feature = "fused", since = "1.26.0")]
278 impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
280 #[unstable(feature = "trusted_len", issue = "37572")]
281 unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
284 #[unstable(issue = "none", feature = "std_internals")]
285 #[rustc_unsafe_specialization_marker]
288 // T: Copy as approximation for !Drop since get_unchecked does not advance self.ptr
289 // and thus we can't implement drop-handling
290 #[unstable(issue = "none", feature = "std_internals")]
291 impl<T: Copy> NonDrop for T {}
294 #[unstable(issue = "none", feature = "std_internals")]
295 // TrustedRandomAccess (without NoCoerce) must not be implemented because
296 // subtypes/supertypes of `T` might not be `NonDrop`
297 unsafe impl<T, A: Allocator> TrustedRandomAccessNoCoerce for IntoIter<T, A>
301 const MAY_HAVE_SIDE_EFFECT: bool = false;
304 #[cfg(not(no_global_oom_handling))]
305 #[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
306 impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> {
308 fn clone(&self) -> Self {
309 self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter()
312 fn clone(&self) -> Self {
313 crate::slice::to_vec(self.as_slice(), self.alloc.deref().clone()).into_iter()
317 #[stable(feature = "rust1", since = "1.0.0")]
318 unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
320 struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>);
322 impl<T, A: Allocator> Drop for DropGuard<'_, T, A> {
325 // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec
326 let alloc = ManuallyDrop::take(&mut self.0.alloc);
327 // RawVec handles deallocation
328 let _ = RawVec::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc);
333 let guard = DropGuard(self);
334 // destroy the remaining elements
336 ptr::drop_in_place(guard.0.as_raw_mut_slice());
338 // now `guard` will be dropped and do the rest
342 // In addition to the SAFETY invariants of the following three unsafe traits
343 // also refer to the vec::in_place_collect module documentation to get an overview
344 #[unstable(issue = "none", feature = "inplace_iteration")]
346 unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> {}
348 #[unstable(issue = "none", feature = "inplace_iteration")]
350 unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> {
354 unsafe fn as_inner(&mut self) -> &mut Self::Source {
359 #[cfg(not(no_global_oom_handling))]
360 unsafe impl<T> AsVecIntoIter for IntoIter<T> {
363 fn as_into_iter(&mut self) -> &mut IntoIter<Self::Item> {