@@ -86,9 +86,10 @@ impl<T> Array<T> {
86
86
self . dims
87
87
. iter ( )
88
88
. zip ( indices. iter ( ) . cloned ( ) )
89
+ . rev ( )
89
90
. fold ( ( 0 , 1 ) , |( acc, stride) , ( dim, idx) | {
90
91
let shifted = dim. shift ( idx) ;
91
- ( acc * stride + shifted, dim. len )
92
+ ( acc + shifted * stride , dim. len * stride )
92
93
} )
93
94
. 0
94
95
}
@@ -160,6 +161,31 @@ tuple_impl!(a: isize, b: isize, c: isize, d: isize, e: isize, f: isize, g: isize
160
161
impl < T , I : ArrayIndex > Index < I > for Array < T > {
161
162
type Output = T ;
162
163
164
+ /// Indexes into the `Array`, retrieving a reference to the contained
165
+ /// value.
166
+ ///
167
+ /// Since `Array`s can be multi-dimensional, the `Index` trait is
168
+ /// implemented for a variety of index types. In the most generic case, a
169
+ /// `&[isize]` can be used. In addition, a bare `isize` as well as tuples
170
+ /// of up to 10 `isize` values may be used for convenience.
171
+ ///
172
+ /// # Panics
173
+ ///
174
+ /// Panics if the index does not correspond to an in-bounds element of the
175
+ /// `Array`.
176
+ ///
177
+ /// # Examples
178
+ ///
179
+ /// ```rust
180
+ /// # use postgres_array::Array;
181
+ /// let mut array = Array::from_vec(vec![0i32, 1, 2, 3], 0);
182
+ /// assert_eq!(2, array[2]);
183
+ ///
184
+ /// array.wrap(0);
185
+ /// array.push(Array::from_vec(vec![4, 5, 6, 7], 0));
186
+ ///
187
+ /// assert_eq!(6, array[(1, 2)]);
188
+ /// ```
163
189
fn index ( & self , idx : I ) -> & T {
164
190
let idx = idx. index ( self ) ;
165
191
& self . data [ idx]
0 commit comments