Skip to content

Commit d263375

Browse files
committed
Document index impl and fix indexing
1 parent 4a6f847 commit d263375

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/array.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ impl<T> Array<T> {
8686
self.dims
8787
.iter()
8888
.zip(indices.iter().cloned())
89+
.rev()
8990
.fold((0, 1), |(acc, stride), (dim, idx)| {
9091
let shifted = dim.shift(idx);
91-
(acc * stride + shifted, dim.len)
92+
(acc + shifted * stride, dim.len * stride)
9293
})
9394
.0
9495
}
@@ -160,6 +161,31 @@ tuple_impl!(a: isize, b: isize, c: isize, d: isize, e: isize, f: isize, g: isize
160161
impl<T, I: ArrayIndex> Index<I> for Array<T> {
161162
type Output = T;
162163

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+
/// ```
163189
fn index(&self, idx: I) -> &T {
164190
let idx = idx.index(self);
165191
&self.data[idx]

0 commit comments

Comments
 (0)