Add elem_ref{_mut}
@@ -94,6 +94,46 @@ impl<S, D, I> IndexMut<I> for ArrayBase<S, D>
}
}
+impl<'a, A, D> ArrayView<'a, A, D>
+where
+ D: Dimension,
+{
+ /// Get a reference of a element through the view.
+ ///
+ /// This is a replacement of `Index::index` since it forces us a wrong lifetime
+ /// https://github.com/bluss/rust-ndarray/issues/371
+ pub fn elem_ref<I: NdIndex<D>>(&self, index: I) -> &'a A {
+ debug_bounds_check!(self, index);
+ unsafe {
+ &*self.as_ptr().offset(
+ index
+ .index_checked(&self.dim, &self.strides)
+ .unwrap_or_else(|| array_out_of_bounds()),
+ )
+ }
+ }
+}
+
+impl<'a, A, D> ArrayViewMut<'a, A, D>
+where
+ D: Dimension,
+{
+ /// Get a mutable reference of a element through the view.
+ ///
+ /// This is a replacement of `IndexMut::index_mut` since it forces us a wrong lifetime
+ /// https://github.com/bluss/rust-ndarray/issues/371
+ pub fn elem_ref_mut<I: NdIndex<D>>(&mut self, index: I) -> &'a mut A {
+ debug_bounds_check!(self, index);
+ unsafe {
+ &mut *self.as_mut_ptr().offset(
+ index
+ .index_checked(&self.dim, &self.strides)
+ .unwrap_or_else(|| array_out_of_bounds()),
+ )
+ }
+ }
+}
+
/// Return `true` if the array shapes and all elements of `self` and
/// `rhs` are equal. Return `false` otherwise.
impl<S, S2, D> PartialEq<ArrayBase<S2, D>> for ArrayBase<S, D>
