1use core::iter::{
2FusedIterator, InPlaceIterable, SourceIter, TrustedFused, TrustedLen,
3 TrustedRandomAccessNoCoerce,
4};
5use core::marker::PhantomData;
6use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties};
7use core::num::NonZero;
8#[cfg(not(no_global_oom_handling))]
9use core::ops::Deref;
10use core::panic::UnwindSafe;
11use core::ptr::{self, NonNull};
12use core::{array, fmt, slice};
1314#[cfg(not(no_global_oom_handling))]
15use super::AsVecIntoIter;
16use crate::alloc::{Allocator, Global};
17#[cfg(not(no_global_oom_handling))]
18use crate::collections::VecDeque;
19use crate::raw_vec::RawVec;
2021macro non_null {
22 (mut $place:expr, $t:ident) => {{
23#![allow(unused_unsafe)] // we're sometimes used within an unsafe block
24 // ignore-tidy-undocumented-unsafe
25unsafe { &mut *((&raw mut $place) as *mut NonNull<$t>) }
26 }},
27 ($place:expr, $t:ident) => {{
28#![allow(unused_unsafe)] // we're sometimes used within an unsafe block
29 // ignore-tidy-undocumented-unsafe
30unsafe { *((&raw const $place) as *const NonNull<$t>) }
31 }},
32}
3334/// An iterator that moves out of a vector.
35///
36/// This `struct` is created by the `into_iter` method on [`Vec`](super::Vec)
37/// (provided by the [`IntoIterator`] trait).
38///
39/// # Example
40///
41/// ```
42/// let v = vec![0, 1, 2];
43/// let iter: std::vec::IntoIter<_> = v.into_iter();
44/// ```
45#[stable(feature = "rust1", since = "1.0.0")]
46#[rustc_insignificant_dtor]
47pub struct IntoIter<
48 T,
49#[stable(feature = "vec_into_iter_allocator", since = "1.101.0")] A: Allocator = Global,
50> {
51pub(super) buf: NonNull<T>,
52pub(super) phantom: PhantomData<T>,
53pub(super) cap: usize,
54// the drop impl reconstructs a RawVec from buf, cap and alloc
55 // to avoid dropping the allocator twice we need to wrap it into ManuallyDrop
56pub(super) alloc: ManuallyDrop<A>,
57pub(super) ptr: NonNull<T>,
58/// If T is a ZST, this is actually ptr+len. This encoding is picked so that
59 /// ptr == end is a quick test for the Iterator being empty, that works
60 /// for both ZST and non-ZST.
61 /// For non-ZSTs the pointer is treated as `NonNull<T>`
62pub(super) end: *const T,
63}
6465// Manually mirroring what `Vec` has,
66// because otherwise we get `T: RefUnwindSafe` from `NonNull`.
67#[stable(feature = "catch_unwind", since = "1.9.0")]
68impl<T: UnwindSafe, A: Allocator + UnwindSafe> UnwindSafefor IntoIter<T, A> {}
6970#[stable(feature = "vec_intoiter_debug", since = "1.13.0")]
71impl<T: fmt::Debug, A: Allocator> fmt::Debugfor IntoIter<T, A> {
72fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
74 }
75}
7677impl<T, A: Allocator> IntoIter<T, A> {
78/// Returns the remaining items of this iterator as a slice.
79 ///
80 /// # Examples
81 ///
82 /// ```
83 /// let vec = vec!['a', 'b', 'c'];
84 /// let mut into_iter = vec.into_iter();
85 /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
86 /// let _ = into_iter.next().unwrap();
87 /// assert_eq!(into_iter.as_slice(), &['b', 'c']);
88 /// ```
89#[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
90pub fn as_slice(&self) -> &[T] {
91// ignore-tidy-undocumented-unsafe
92unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len()) }
93 }
9495/// Returns the remaining items of this iterator as a mutable slice.
96 ///
97 /// # Examples
98 ///
99 /// ```
100 /// let vec = vec!['a', 'b', 'c'];
101 /// let mut into_iter = vec.into_iter();
102 /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
103 /// into_iter.as_mut_slice()[2] = 'z';
104 /// assert_eq!(into_iter.next().unwrap(), 'a');
105 /// assert_eq!(into_iter.next().unwrap(), 'b');
106 /// assert_eq!(into_iter.next().unwrap(), 'z');
107 /// ```
108#[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
109pub fn as_mut_slice(&mut self) -> &mut [T] {
110// ignore-tidy-undocumented-unsafe
111unsafe { &mut *self.as_raw_mut_slice() }
112 }
113114/// Returns a reference to the underlying allocator.
115#[stable(feature = "vec_into_iter_allocator", since = "1.101.0")]
116 #[inline]
117pub fn allocator(&self) -> &A {
118&self.alloc
119 }
120121fn as_raw_mut_slice(&mut self) -> *mut [T] {
122self.ptr.as_ptr().cast_slice(self.len())
123 }
124125/// Drops remaining elements and relinquishes the backing allocation.
126 ///
127 /// This method guarantees it won't panic before relinquishing the backing
128 /// allocation.
129 ///
130 /// This is roughly equivalent to the following, but more efficient
131 ///
132 /// ```
133 /// # let mut vec = Vec::<u8>::with_capacity(10);
134 /// # let ptr = vec.as_mut_ptr();
135 /// # let mut into_iter = vec.into_iter();
136 /// let mut into_iter = std::mem::replace(&mut into_iter, Vec::new().into_iter());
137 /// (&mut into_iter).for_each(drop);
138 /// std::mem::forget(into_iter);
139 /// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
140 /// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
141 /// # drop(unsafe { Vec::<u8>::from_raw_parts(ptr, 0, 10) });
142 /// ```
143 ///
144 /// This method is used by in-place iteration, refer to the vec::in_place_collect
145 /// documentation for an overview.
146#[cfg(not(no_global_oom_handling))]
147pub(super) fn forget_allocation_drop_remaining(&mut self) {
148let remaining = self.as_raw_mut_slice();
149150// overwrite the individual fields instead of creating a new
151 // struct and then overwriting &mut self.
152 // this creates less assembly
153self.cap = 0;
154self.buf = RawVec::new().non_null();
155self.ptr = self.buf;
156self.end = self.buf.as_ptr();
157158// Dropping the remaining elements can panic, so this needs to be
159 // done only after updating the other fields.
160 // ignore-tidy-undocumented-unsafe
161unsafe {
162 ptr::drop_in_place(remaining);
163 }
164 }
165166/// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed.
167 ///
168 /// This method does not consume `self`, and leaves deallocation to `impl Drop for IntoIter`.
169 /// If consuming `self` is possible, consider calling
170 /// [`Self::forget_remaining_elements_and_dealloc()`] instead.
171pub(crate) fn forget_remaining_elements(&mut self) {
172// For the ZST case, it is crucial that we mutate `end` here, not `ptr`.
173 // `ptr` must stay aligned, while `end` may be unaligned.
174self.end = self.ptr.as_ptr();
175 }
176177/// Forgets to Drop the remaining elements and frees the backing allocation.
178 /// Consuming version of [`Self::forget_remaining_elements()`].
179 ///
180 /// This can be used in place of `drop(self)` when `self` is known to be exhausted,
181 /// to avoid producing a needless `drop_in_place::<[T]>()`.
182#[inline]
183pub(crate) fn forget_remaining_elements_and_dealloc(self) {
184let mut this = ManuallyDrop::new(self);
185// SAFETY: `this` is in ManuallyDrop, so it will not be double-freed.
186unsafe {
187this.dealloc_only();
188 }
189 }
190191/// Frees the allocation, without checking or dropping anything else.
192 ///
193 /// The safe version of this method is [`Self::forget_remaining_elements_and_dealloc()`].
194 /// This function exists only to share code between that method and the `impl Drop`.
195 ///
196 /// # Safety
197 ///
198 /// This function must only be called with an [`IntoIter`] that is not going to be dropped
199 /// or otherwise used in any way, either because it is being forgotten or because its `Drop`
200 /// is already executing; otherwise a double-free will occur, and possibly a read from freed
201 /// memory if there are any remaining elements.
202#[inline]
203unsafe fn dealloc_only(&mut self) {
204// SAFETY: our caller promises not to touch `*self` again.
205let alloc = unsafe { ManuallyDrop::take(&mut self.alloc) };
206// SAFETY: We're using this to deallocate a preexisting `RawVec`.
207let _ = unsafe { RawVec::from_nonnull_in(self.buf, self.cap, alloc) };
208 }
209210#[cfg(not(no_global_oom_handling))]
211 #[inline]
212pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> {
213// Keep our `Drop` impl from dropping the elements and the allocator
214let mut this = ManuallyDrop::new(self);
215216let buf = this.buf.as_ptr();
217let initialized = if T::IS_ZST || this.len() == 0 {
218// All the pointers are the same for ZSTs, so it's fine to
219 // say that they're all at the beginning of the "allocation".
220 // For non-ZSTs, we have length 0, so we can choose the (empty)
221 // range to be at the start of the buffer.
222 //
223 // Due to `0` ≤ `this.len()` ≤ `this.cap`, the range is well-formed,
224 // and due to the argument above it spans exactly the elements of
225 // this iterator. Because `init.start` = `0`, it follows that either
226 // `init.start` < `cap` or `cap` = `init.start` = `0`; thus the range
227 // satisfies the requirements of `from_contiguous_raw_parts_in`.
2280..this.len()
229 } else {
230// SAFETY: `this.ptr` and `this.end` are created via offsets of `this.buf`,
231 // so they point to the same allocation. We have `this.buf` ≤ `this.ptr` ≤ `this.end`,
232 // so this cannot wrap, and will produce a well-formed range that spans exactly
233 // the elements of this iterator.
234 //
235 // Additionally, due to `end ≤ buf + cap`, we have `init.start` ≤ `init.end` ≤ `cap`.
236 // Due to the length check above, `init.start < cap`, so the range satisfies the
237 // requirements of `from_contiguous_raw_parts_in`.
238unsafe { this.ptr.offset_from_unsigned(this.buf)..this.end.offset_from_unsigned(buf) }
239 };
240241let cap = this.cap;
242// SAFETY: `this` is forgotten afterwards, so we can move out the allocator.
243let alloc = unsafe { ManuallyDrop::take(&mut this.alloc) };
244245// SAFETY: This allocation originally came from a `Vec`, so it satisfies all
246 // requirements for the `buf` pointer with capacity `cap` allocated in `alloc`.
247 // Correctness of `initialized` was shown above.
248unsafe { VecDeque::from_contiguous_raw_parts_in(buf, initialized, cap, alloc) }
249 }
250}
251252#[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")]
253impl<T, A: Allocator> AsRef<[T]> for IntoIter<T, A> {
254fn as_ref(&self) -> &[T] {
255self.as_slice()
256 }
257}
258259#[stable(feature = "rust1", since = "1.0.0")]
260unsafe impl<T: Send, A: Allocator + Send> Sendfor IntoIter<T, A> {}
261#[stable(feature = "rust1", since = "1.0.0")]
262unsafe impl<T: Sync, A: Allocator + Sync> Syncfor IntoIter<T, A> {}
263264#[stable(feature = "rust1", since = "1.0.0")]
265impl<T, A: Allocator> Iteratorfor IntoIter<T, A> {
266type Item = T;
267268#[inline]
269fn next(&mut self) -> Option<T> {
270let ptr = if T::IS_ZST {
271if self.ptr.as_ptr() == self.end as *mut T {
272return None;
273 }
274// `ptr` has to stay where it is to remain aligned, so we reduce the length by 1 by
275 // reducing the `end`.
276self.end = self.end.wrapping_byte_sub(1);
277self.ptr
278 } else {
279if self.ptr == {
#![allow(unused_unsafe)]
unsafe { *((&raw const self.end) as *const NonNull<T>) }
}non_null!(self.end, T) {
280return None;
281 }
282let old = self.ptr;
283// ignore-tidy-undocumented-unsafe
284self.ptr = unsafe { old.add(1) };
285old286 };
287// ignore-tidy-undocumented-unsafe
288Some(unsafe { ptr.read() })
289 }
290291#[inline]
292fn size_hint(&self) -> (usize, Option<usize>) {
293let exact = if T::IS_ZST {
294self.end.addr().wrapping_sub(self.ptr.as_ptr().addr())
295 } else {
296// ignore-tidy-undocumented-unsafe
297unsafe { {
#![allow(unused_unsafe)]
unsafe { *((&raw const self.end) as *const NonNull<T>) }
}non_null!(self.end, T).offset_from_unsigned(self.ptr) }
298 };
299 (exact, Some(exact))
300 }
301302#[inline]
303fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
304let step_size = self.len().min(n);
305let to_drop = self.ptr.as_ptr().cast_slice(step_size);
306if T::IS_ZST {
307// See `next` for why we sub `end` here.
308self.end = self.end.wrapping_byte_sub(step_size);
309 } else {
310// SAFETY: the min() above ensures that step_size is in bounds
311self.ptr = unsafe { self.ptr.add(step_size) };
312 }
313// SAFETY: the min() above ensures that step_size is in bounds
314unsafe {
315 ptr::drop_in_place(to_drop);
316 }
317NonZero::new(n - step_size).map_or(Ok(()), Err)
318 }
319320#[inline]
321fn count(self) -> usize {
322self.len()
323 }
324325#[inline]
326fn last(mut self) -> Option<T> {
327self.next_back()
328 }
329330#[inline]
331fn next_chunk<const N: usize>(&mut self) -> Result<[T; N], core::array::IntoIter<T, N>> {
332let mut raw_ary = [const { MaybeUninit::uninit() }; N];
333334let len = self.len();
335336if T::IS_ZST {
337if len < N {
338self.forget_remaining_elements();
339// SAFETY: ZSTs can be conjured ex nihilo, only the amount has to be correct
340return Err(unsafe { array::IntoIter::new_unchecked(raw_ary, 0..len) });
341 }
342343self.end = self.end.wrapping_byte_sub(N);
344// SAFETY: ditto
345return Ok(unsafe { raw_ary.transpose().assume_init() });
346 }
347348if len < N {
349// SAFETY: `len` indicates that this many elements are available and we
350 // just checked that it fits into the array.
351unsafe {
352 ptr::copy_nonoverlapping(self.ptr.as_ptr(), raw_ary.as_mut_ptr() as *mut T, len);
353self.forget_remaining_elements();
354return Err(array::IntoIter::new_unchecked(raw_ary, 0..len));
355 }
356 }
357358// SAFETY: `len` is larger than the array size. Copy a fixed amount here to fully initialize
359 // the array.
360unsafe {
361 ptr::copy_nonoverlapping(self.ptr.as_ptr(), raw_ary.as_mut_ptr() as *mut T, N);
362self.ptr = self.ptr.add(N);
363Ok(raw_ary.transpose().assume_init())
364 }
365 }
366367#[inline]
368fn fold<B, F>(mut self, mut accum: B, mut f: F) -> B
369where
370F: FnMut(B, Self::Item) -> B,
371 {
372if T::IS_ZST {
373while self.ptr.as_ptr() != self.end.cast_mut() {
374// SAFETY: we just checked that `self.ptr` is in bounds.
375let tmp = unsafe { self.ptr.read() };
376// See `next` for why we subtract from `end` here.
377self.end = self.end.wrapping_byte_sub(1);
378 accum = f(accum, tmp);
379 }
380 } else {
381// SAFETY: `self.end` can only be null if `T` is a ZST.
382while self.ptr != {
#![allow(unused_unsafe)]
unsafe { *((&raw const self.end) as *const NonNull<T>) }
}non_null!(self.end, T) {
383// SAFETY: we just checked that `self.ptr` is in bounds.
384let tmp = unsafe { self.ptr.read() };
385// SAFETY: the maximum this can be is `self.end`.
386 // Increment `self.ptr` first to avoid double dropping in the event of a panic.
387self.ptr = unsafe { self.ptr.add(1) };
388 accum = f(accum, tmp);
389 }
390 }
391392// There are in fact no remaining elements to forget, but by doing this we can avoid
393 // potentially generating a needless loop to drop the elements that cannot exist at
394 // this point.
395self.forget_remaining_elements_and_dealloc();
396397accum398 }
399400#[inline]
401fn try_fold<B, F, R>(&mut self, mut accum: B, mut f: F) -> R
402where
403Self: Sized,
404 F: FnMut(B, Self::Item) -> R,
405 R: core::ops::Try<Output = B>,
406 {
407if T::IS_ZST {
408while self.ptr.as_ptr() != self.end.cast_mut() {
409// SAFETY: we just checked that `self.ptr` is in bounds.
410let tmp = unsafe { self.ptr.read() };
411// See `next` for why we subtract from `end` here.
412self.end = self.end.wrapping_byte_sub(1);
413 accum = f(accum, tmp)?;
414 }
415 } else {
416// SAFETY: `self.end` can only be null if `T` is a ZST.
417while self.ptr != {
#![allow(unused_unsafe)]
unsafe { *((&raw const self.end) as *const NonNull<T>) }
}non_null!(self.end, T) {
418// SAFETY: we just checked that `self.ptr` is in bounds.
419let tmp = unsafe { self.ptr.read() };
420// SAFETY: the maximum this can be is `self.end`.
421 // Increment `self.ptr` first to avoid double dropping in the event of a panic.
422self.ptr = unsafe { self.ptr.add(1) };
423 accum = f(accum, tmp)?;
424 }
425 }
426 R::from_output(accum)
427 }
428429unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> Self::Item
430where
431Self: TrustedRandomAccessNoCoerce,
432 {
433// SAFETY: the caller must guarantee that `i` is in bounds of the
434 // `Vec<T>`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)`
435 // is guaranteed to pointer to an element of the `Vec<T>` and
436 // thus guaranteed to be valid to dereference.
437 //
438 // Also note the implementation of `Self: TrustedRandomAccess` requires
439 // that `T: Copy` so reading elements from the buffer doesn't invalidate
440 // them for `Drop`.
441unsafe { self.ptr.add(i).read() }
442 }
443}
444445#[stable(feature = "rust1", since = "1.0.0")]
446impl<T, A: Allocator> DoubleEndedIteratorfor IntoIter<T, A> {
447#[inline]
448fn next_back(&mut self) -> Option<T> {
449if T::IS_ZST {
450if self.ptr.as_ptr() == self.end as *mut _ {
451return None;
452 }
453// See above for why 'ptr.offset' isn't used
454self.end = self.end.wrapping_byte_sub(1);
455// Note that even though this is next_back() we're reading from `self.ptr`, not
456 // `self.end`. We track our length using the byte offset from `self.ptr` to `self.end`,
457 // so the end pointer may not be suitably aligned for T.
458 // ignore-tidy-undocumented-unsafe
459Some(unsafe { ptr::read(self.ptr.as_ptr()) })
460 } else {
461if self.ptr == {
#![allow(unused_unsafe)]
unsafe { *((&raw const self.end) as *const NonNull<T>) }
}non_null!(self.end, T) {
462return None;
463 }
464// ignore-tidy-undocumented-unsafe
465unsafe {
466self.end = self.end.sub(1);
467Some(ptr::read(self.end))
468 }
469 }
470 }
471472#[inline]
473fn next_chunk_back<const N: usize>(&mut self) -> Result<[T; N], core::array::IntoIter<T, N>> {
474let mut raw_ary = [const { MaybeUninit::uninit() }; N];
475476let len = self.len();
477478if T::IS_ZST {
479if len < N {
480self.forget_remaining_elements();
481// SAFETY: ZSTs can be conjured ex nihilo, only the amount has to be correct
482return Err(unsafe { array::IntoIter::new_unchecked(raw_ary, N - len..N) });
483 }
484485self.end = self.end.wrapping_byte_sub(N);
486// SAFETY: ditto
487return Ok(unsafe { MaybeUninit::array_assume_init(raw_ary) });
488 }
489490if len < N {
491// SAFETY: `len` indicates that this many elements are available
492 // and we just checked that it fits into the array.
493unsafe {
494 ptr::copy_nonoverlapping(self.ptr.as_ptr(), raw_ary.as_mut_ptr() as *mut T, len);
495self.forget_remaining_elements();
496return Err(array::IntoIter::new_unchecked(raw_ary, 0..len));
497 }
498 }
499500// SAFETY: `len` is larger than the array size. Copy a fixed amount here to fully initialize
501 // the array.
502unsafe {
503 ptr::copy_nonoverlapping(
504self.ptr.add(len - N).as_ptr(),
505raw_ary.as_mut_ptr() as *mut T,
506N,
507 );
508self.end = self.end.sub(N);
509Ok(MaybeUninit::array_assume_init(raw_ary))
510 }
511 }
512513#[inline]
514fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
515let step_size = self.len().min(n);
516if T::IS_ZST {
517// SAFETY: same as for advance_by()
518self.end = self.end.wrapping_byte_sub(step_size);
519 } else {
520// SAFETY: same as for advance_by()
521self.end = unsafe { self.end.sub(step_size) };
522 }
523let to_drop = if T::IS_ZST {
524// ZST may cause unalignment
525ptr::NonNull::<T>::dangling().as_ptr().cast_slice(step_size)
526 } else {
527self.end.cast::<T>().cast_mut().cast_slice(step_size)
528 };
529// SAFETY: same as for advance_by()
530unsafe {
531 ptr::drop_in_place(to_drop);
532 }
533NonZero::new(n - step_size).map_or(Ok(()), Err)
534 }
535}
536537#[stable(feature = "rust1", since = "1.0.0")]
538impl<T, A: Allocator> ExactSizeIteratorfor IntoIter<T, A> {
539fn is_empty(&self) -> bool {
540if T::IS_ZST {
541self.ptr.as_ptr() == self.end as *mut _
542} else {
543self.ptr == {
#![allow(unused_unsafe)]
unsafe { *((&raw const self.end) as *const NonNull<T>) }
}non_null!(self.end, T)544 }
545 }
546}
547548#[stable(feature = "fused", since = "1.26.0")]
549impl<T, A: Allocator> FusedIteratorfor IntoIter<T, A> {}
550551#[doc(hidden)]
552#[unstable(issue = "none", feature = "trusted_fused")]
553unsafe impl<T, A: Allocator> TrustedFused for IntoIter<T, A> {}
554555#[unstable(feature = "trusted_len", issue = "37572")]
556unsafe impl<T, A: Allocator> TrustedLenfor IntoIter<T, A> {}
557558#[stable(feature = "default_iters", since = "1.70.0")]
559impl<T, A> Defaultfor IntoIter<T, A>
560where
561A: Allocator + Default,
562{
563/// Creates an empty `vec::IntoIter`.
564 ///
565 /// ```
566 /// # use std::vec;
567 /// let iter: vec::IntoIter<u8> = Default::default();
568 /// assert_eq!(iter.len(), 0);
569 /// assert_eq!(iter.as_slice(), &[]);
570 /// ```
571fn default() -> Self {
572super::Vec::new_in(Default::default()).into_iter()
573 }
574}
575576#[doc(hidden)]
577#[unstable(issue = "none", feature = "std_internals")]
578#[unsafe(rustc_allow_lifetime_dependent_specialization)]
579trait NonDrop {}
580581// T: Copy as approximation for !Drop since get_unchecked does not advance self.ptr
582// and thus we can't implement drop-handling
583#[unstable(issue = "none", feature = "std_internals")]
584impl<T: Copy> NonDropfor T {}
585586#[doc(hidden)]
587#[unstable(issue = "none", feature = "std_internals")]
588// TrustedRandomAccess (without NoCoerce) must not be implemented because
589// subtypes/supertypes of `T` might not be `NonDrop`
590unsafe impl<T, A: Allocator> TrustedRandomAccessNoCoerce for IntoIter<T, A>
591where
592T: NonDrop,
593{
594const MAY_HAVE_SIDE_EFFECT: bool = false;
595}
596597#[cfg(not(no_global_oom_handling))]
598#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
599impl<T: Clone, A: Allocator + Clone> Clonefor IntoIter<T, A> {
600fn clone(&self) -> Self {
601self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter()
602 }
603}
604605#[stable(feature = "rust1", since = "1.0.0")]
606unsafe impl<#[may_dangle] T, A: Allocator> Dropfor IntoIter<T, A> {
607fn drop(&mut self) {
608struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>);
609610impl<T, A: Allocator> Dropfor DropGuard<'_, T, A> {
611fn drop(&mut self) {
612// ignore-tidy-undocumented-unsafe
613unsafe {
614self.0.dealloc_only();
615 }
616 }
617 }
618619let guard = DropGuard(self);
620// destroy the remaining elements
621 // ignore-tidy-undocumented-unsafe
622unsafe {
623 ptr::drop_in_place(guard.0.as_raw_mut_slice());
624 }
625// now `guard` will be dropped and do the rest
626}
627}
628629// In addition to the SAFETY invariants of the following three unsafe traits
630// also refer to the vec::in_place_collect module documentation to get an overview
631#[unstable(issue = "none", feature = "inplace_iteration")]
632#[doc(hidden)]
633unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> {
634const EXPAND_BY: Option<NonZero<usize>> = NonZero::new(1);
635const MERGE_BY: Option<NonZero<usize>> = NonZero::new(1);
636}
637638#[unstable(issue = "none", feature = "inplace_iteration")]
639#[doc(hidden)]
640unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> {
641type Source = Self;
642643#[inline]
644unsafe fn as_inner(&mut self) -> &mut Self::Source {
645self646 }
647}
648649#[cfg(not(no_global_oom_handling))]
650unsafe impl<T> AsVecIntoIterfor IntoIter<T> {
651type Item = T;
652653fn as_into_iter(&mut self) -> &mut IntoIter<Self::Item> {
654self655 }
656}