|  | 
|  | 1 | +use core::fmt; | 
|  | 2 | +use core::iter::FusedIterator; | 
|  | 3 | +use core::ops::Try; | 
|  | 4 | + | 
|  | 5 | +use super::{count, wrap_index, RingSlices}; | 
|  | 6 | + | 
|  | 7 | +/// An iterator over the elements of a `VecDeque`. | 
|  | 8 | +/// | 
|  | 9 | +/// This `struct` is created by the [`iter`] method on [`super::VecDeque`]. See its | 
|  | 10 | +/// documentation for more. | 
|  | 11 | +/// | 
|  | 12 | +/// [`iter`]: super::VecDeque::iter | 
|  | 13 | +#[stable(feature = "rust1", since = "1.0.0")] | 
|  | 14 | +pub struct Iter<'a, T: 'a> { | 
|  | 15 | +    pub(crate) ring: &'a [T], | 
|  | 16 | +    pub(crate) tail: usize, | 
|  | 17 | +    pub(crate) head: usize, | 
|  | 18 | +} | 
|  | 19 | + | 
|  | 20 | +#[stable(feature = "collection_debug", since = "1.17.0")] | 
|  | 21 | +impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> { | 
|  | 22 | +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 
|  | 23 | +        let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); | 
|  | 24 | +        f.debug_tuple("Iter").field(&front).field(&back).finish() | 
|  | 25 | +    } | 
|  | 26 | +} | 
|  | 27 | + | 
|  | 28 | +// FIXME(#26925) Remove in favor of `#[derive(Clone)]` | 
|  | 29 | +#[stable(feature = "rust1", since = "1.0.0")] | 
|  | 30 | +impl<T> Clone for Iter<'_, T> { | 
|  | 31 | +    fn clone(&self) -> Self { | 
|  | 32 | +        Iter { ring: self.ring, tail: self.tail, head: self.head } | 
|  | 33 | +    } | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +#[stable(feature = "rust1", since = "1.0.0")] | 
|  | 37 | +impl<'a, T> Iterator for Iter<'a, T> { | 
|  | 38 | +    type Item = &'a T; | 
|  | 39 | + | 
|  | 40 | +    #[inline] | 
|  | 41 | +    fn next(&mut self) -> Option<&'a T> { | 
|  | 42 | +        if self.tail == self.head { | 
|  | 43 | +            return None; | 
|  | 44 | +        } | 
|  | 45 | +        let tail = self.tail; | 
|  | 46 | +        self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len()); | 
|  | 47 | +        unsafe { Some(self.ring.get_unchecked(tail)) } | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    #[inline] | 
|  | 51 | +    fn size_hint(&self) -> (usize, Option<usize>) { | 
|  | 52 | +        let len = count(self.tail, self.head, self.ring.len()); | 
|  | 53 | +        (len, Some(len)) | 
|  | 54 | +    } | 
|  | 55 | + | 
|  | 56 | +    fn fold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc | 
|  | 57 | +    where | 
|  | 58 | +        F: FnMut(Acc, Self::Item) -> Acc, | 
|  | 59 | +    { | 
|  | 60 | +        let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); | 
|  | 61 | +        accum = front.iter().fold(accum, &mut f); | 
|  | 62 | +        back.iter().fold(accum, &mut f) | 
|  | 63 | +    } | 
|  | 64 | + | 
|  | 65 | +    fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R | 
|  | 66 | +    where | 
|  | 67 | +        Self: Sized, | 
|  | 68 | +        F: FnMut(B, Self::Item) -> R, | 
|  | 69 | +        R: Try<Ok = B>, | 
|  | 70 | +    { | 
|  | 71 | +        let (mut iter, final_res); | 
|  | 72 | +        if self.tail <= self.head { | 
|  | 73 | +            // single slice self.ring[self.tail..self.head] | 
|  | 74 | +            iter = self.ring[self.tail..self.head].iter(); | 
|  | 75 | +            final_res = iter.try_fold(init, &mut f); | 
|  | 76 | +        } else { | 
|  | 77 | +            // two slices: self.ring[self.tail..], self.ring[..self.head] | 
|  | 78 | +            let (front, back) = self.ring.split_at(self.tail); | 
|  | 79 | +            let mut back_iter = back.iter(); | 
|  | 80 | +            let res = back_iter.try_fold(init, &mut f); | 
|  | 81 | +            let len = self.ring.len(); | 
|  | 82 | +            self.tail = (self.ring.len() - back_iter.len()) & (len - 1); | 
|  | 83 | +            iter = front[..self.head].iter(); | 
|  | 84 | +            final_res = iter.try_fold(res?, &mut f); | 
|  | 85 | +        } | 
|  | 86 | +        self.tail = self.head - iter.len(); | 
|  | 87 | +        final_res | 
|  | 88 | +    } | 
|  | 89 | + | 
|  | 90 | +    fn nth(&mut self, n: usize) -> Option<Self::Item> { | 
|  | 91 | +        if n >= count(self.tail, self.head, self.ring.len()) { | 
|  | 92 | +            self.tail = self.head; | 
|  | 93 | +            None | 
|  | 94 | +        } else { | 
|  | 95 | +            self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len()); | 
|  | 96 | +            self.next() | 
|  | 97 | +        } | 
|  | 98 | +    } | 
|  | 99 | + | 
|  | 100 | +    #[inline] | 
|  | 101 | +    fn last(mut self) -> Option<&'a T> { | 
|  | 102 | +        self.next_back() | 
|  | 103 | +    } | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +#[stable(feature = "rust1", since = "1.0.0")] | 
|  | 107 | +impl<'a, T> DoubleEndedIterator for Iter<'a, T> { | 
|  | 108 | +    #[inline] | 
|  | 109 | +    fn next_back(&mut self) -> Option<&'a T> { | 
|  | 110 | +        if self.tail == self.head { | 
|  | 111 | +            return None; | 
|  | 112 | +        } | 
|  | 113 | +        self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len()); | 
|  | 114 | +        unsafe { Some(self.ring.get_unchecked(self.head)) } | 
|  | 115 | +    } | 
|  | 116 | + | 
|  | 117 | +    fn rfold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc | 
|  | 118 | +    where | 
|  | 119 | +        F: FnMut(Acc, Self::Item) -> Acc, | 
|  | 120 | +    { | 
|  | 121 | +        let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); | 
|  | 122 | +        accum = back.iter().rfold(accum, &mut f); | 
|  | 123 | +        front.iter().rfold(accum, &mut f) | 
|  | 124 | +    } | 
|  | 125 | + | 
|  | 126 | +    fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R | 
|  | 127 | +    where | 
|  | 128 | +        Self: Sized, | 
|  | 129 | +        F: FnMut(B, Self::Item) -> R, | 
|  | 130 | +        R: Try<Ok = B>, | 
|  | 131 | +    { | 
|  | 132 | +        let (mut iter, final_res); | 
|  | 133 | +        if self.tail <= self.head { | 
|  | 134 | +            // single slice self.ring[self.tail..self.head] | 
|  | 135 | +            iter = self.ring[self.tail..self.head].iter(); | 
|  | 136 | +            final_res = iter.try_rfold(init, &mut f); | 
|  | 137 | +        } else { | 
|  | 138 | +            // two slices: self.ring[self.tail..], self.ring[..self.head] | 
|  | 139 | +            let (front, back) = self.ring.split_at(self.tail); | 
|  | 140 | +            let mut front_iter = front[..self.head].iter(); | 
|  | 141 | +            let res = front_iter.try_rfold(init, &mut f); | 
|  | 142 | +            self.head = front_iter.len(); | 
|  | 143 | +            iter = back.iter(); | 
|  | 144 | +            final_res = iter.try_rfold(res?, &mut f); | 
|  | 145 | +        } | 
|  | 146 | +        self.head = self.tail + iter.len(); | 
|  | 147 | +        final_res | 
|  | 148 | +    } | 
|  | 149 | +} | 
|  | 150 | + | 
|  | 151 | +#[stable(feature = "rust1", since = "1.0.0")] | 
|  | 152 | +impl<T> ExactSizeIterator for Iter<'_, T> { | 
|  | 153 | +    fn is_empty(&self) -> bool { | 
|  | 154 | +        self.head == self.tail | 
|  | 155 | +    } | 
|  | 156 | +} | 
|  | 157 | + | 
|  | 158 | +#[stable(feature = "fused", since = "1.26.0")] | 
|  | 159 | +impl<T> FusedIterator for Iter<'_, T> {} | 
0 commit comments