]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/collections/vec_deque/into_iter.rs
Rollup merge of #86152 - the8472:lazify-npm-queries, r=Mark-Simulacrum
[rust.git] / library / alloc / src / collections / vec_deque / into_iter.rs
1 use core::fmt;
2 use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
3
4 use super::VecDeque;
5
6 /// An owning iterator over the elements of a `VecDeque`.
7 ///
8 /// This `struct` is created by the [`into_iter`] method on [`VecDeque`]
9 /// (provided by the `IntoIterator` trait). See its documentation for more.
10 ///
11 /// [`into_iter`]: VecDeque::into_iter
12 #[derive(Clone)]
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub struct IntoIter<T> {
15     pub(crate) inner: VecDeque<T>,
16 }
17
18 #[stable(feature = "collection_debug", since = "1.17.0")]
19 impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
20     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21         f.debug_tuple("IntoIter").field(&self.inner).finish()
22     }
23 }
24
25 #[stable(feature = "rust1", since = "1.0.0")]
26 impl<T> Iterator for IntoIter<T> {
27     type Item = T;
28
29     #[inline]
30     fn next(&mut self) -> Option<T> {
31         self.inner.pop_front()
32     }
33
34     #[inline]
35     fn size_hint(&self) -> (usize, Option<usize>) {
36         let len = self.inner.len();
37         (len, Some(len))
38     }
39
40     #[inline]
41     #[doc(hidden)]
42     unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
43     where
44         Self: TrustedRandomAccess,
45     {
46         // Safety: The TrustedRandomAccess contract requires that callers only pass an index
47         // that is in bounds.
48         // Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even
49         // multiple repeated reads of the same index would be safe and the
50         // values are !Drop, thus won't suffer from double drops.
51         unsafe {
52             let idx = self.inner.wrap_add(self.inner.tail, idx);
53             self.inner.buffer_read(idx)
54         }
55     }
56 }
57
58 #[stable(feature = "rust1", since = "1.0.0")]
59 impl<T> DoubleEndedIterator for IntoIter<T> {
60     #[inline]
61     fn next_back(&mut self) -> Option<T> {
62         self.inner.pop_back()
63     }
64 }
65
66 #[stable(feature = "rust1", since = "1.0.0")]
67 impl<T> ExactSizeIterator for IntoIter<T> {
68     fn is_empty(&self) -> bool {
69         self.inner.is_empty()
70     }
71 }
72
73 #[stable(feature = "fused", since = "1.26.0")]
74 impl<T> FusedIterator for IntoIter<T> {}
75
76 #[unstable(feature = "trusted_len", issue = "37572")]
77 unsafe impl<T> TrustedLen for IntoIter<T> {}
78
79 #[doc(hidden)]
80 #[unstable(feature = "trusted_random_access", issue = "none")]
81 // T: Copy as approximation for !Drop since get_unchecked does not update the pointers
82 // and thus we can't implement drop-handling
83 unsafe impl<T> TrustedRandomAccess for IntoIter<T>
84 where
85     T: Copy,
86 {
87     const MAY_HAVE_SIDE_EFFECT: bool = false;
88 }