@@ -97,42 +97,64 @@ impl<T: ImplicitClone + 'static> From<[T; 1]> for IArray<T> {
9797#[ derive( Debug ) ]
9898pub struct Iter < T : ImplicitClone + ' static > {
9999 array : IArray < T > ,
100- index : usize ,
100+ left : usize ,
101+ right : usize ,
101102}
102103
103104impl < T : ImplicitClone + ' static > Iter < T > {
104105 fn new ( array : IArray < T > ) -> Self {
105- Self { array, index : 0 }
106+ Self {
107+ left : 0 ,
108+ right : array. len ( ) ,
109+ array,
110+ }
106111 }
107112}
108113
109114impl < T : ImplicitClone + ' static > Iterator for Iter < T > {
110115 type Item = T ;
111116
112117 fn next ( & mut self ) -> Option < Self :: Item > {
113- let item = self . array . get ( self . index ) ;
114- self . index += 1 ;
118+ if self . left >= self . right {
119+ return None ;
120+ }
121+ let item = self . array . get ( self . left ) ;
122+ self . left += 1 ;
115123 item
116124 }
117125}
118126
127+ impl < T : ImplicitClone + ' static > DoubleEndedIterator for Iter < T > {
128+ fn next_back ( & mut self ) -> Option < Self :: Item > {
129+ if self . left >= self . right {
130+ return None ;
131+ }
132+ self . right -= 1 ;
133+ self . array . get ( self . right )
134+ }
135+ }
136+
119137impl < T : ImplicitClone + ' static > IArray < T > {
120138 /// An empty array without allocation.
121139 pub const EMPTY : Self = Self :: Static ( & [ ] ) ;
122140
123- /// Returns an iterator over the slice .
141+ /// Returns a double-ended iterator over the array .
124142 ///
125143 /// # Examples
126144 ///
127145 /// ```
128146 /// # use implicit_clone::unsync::*;
129- /// let x = IArray::<u8>::Static(&[1, 2, 4 ]);
130- /// let mut iterator = x.iter();
147+ /// let x = IArray::<u8>::Static(&[1, 2, 3, 4, 5, 6 ]);
148+ /// let mut iter = x.iter();
131149 ///
132- /// assert_eq!(iterator.next(), Some(1));
133- /// assert_eq!(iterator.next(), Some(2));
134- /// assert_eq!(iterator.next(), Some(4));
135- /// assert_eq!(iterator.next(), None);
150+ /// assert_eq!(Some(1), iter.next());
151+ /// assert_eq!(Some(6), iter.next_back());
152+ /// assert_eq!(Some(5), iter.next_back());
153+ /// assert_eq!(Some(2), iter.next());
154+ /// assert_eq!(Some(3), iter.next());
155+ /// assert_eq!(Some(4), iter.next());
156+ /// assert_eq!(None, iter.next());
157+ /// assert_eq!(None, iter.next_back());
136158 /// ```
137159 #[ inline]
138160 pub fn iter ( & self ) -> Iter < T > {
0 commit comments