]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/collections/vec_deque/into_iter.rs
Rollup merge of #80269 - pickfire:patch-4, r=joshtriplett
[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     unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
42     where
43         Self: TrustedRandomAccess,
44     {
45         // Safety: The TrustedRandomAccess contract requires that callers only pass an index
46         // that is in bounds.
47         // Additionally Self: TrustedRandomAccess is only implemented for T: Copy which means even
48         // multiple repeated reads of the same index would be safe and the
49         // values are !Drop, thus won't suffer from double drops.
50         unsafe {
51             let idx = self.inner.wrap_add(self.inner.tail, idx);
52             self.inner.buffer_read(idx)
53         }
54     }
55 }
56
57 #[stable(feature = "rust1", since = "1.0.0")]
58 impl<T> DoubleEndedIterator for IntoIter<T> {
59     #[inline]
60     fn next_back(&mut self) -> Option<T> {
61         self.inner.pop_back()
62     }
63 }
64
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl<T> ExactSizeIterator for IntoIter<T> {
67     fn is_empty(&self) -> bool {
68         self.inner.is_empty()
69     }
70 }
71
72 #[stable(feature = "fused", since = "1.26.0")]
73 impl<T> FusedIterator for IntoIter<T> {}
74
75 #[unstable(feature = "trusted_len", issue = "37572")]
76 unsafe impl<T> TrustedLen for IntoIter<T> {}
77
78 #[doc(hidden)]
79 #[unstable(feature = "trusted_random_access", issue = "none")]
80 // T: Copy as approximation for !Drop since get_unchecked does not update the pointers
81 // and thus we can't implement drop-handling
82 unsafe impl<T> TrustedRandomAccess for IntoIter<T>
83 where
84     T: Copy,
85 {
86     const MAY_HAVE_SIDE_EFFECT: bool = false;
87 }