Skip to content

Commit 272d4d8

Browse files
committed
Add elem_ref{_mut}
1 parent f2c3c48 commit 272d4d8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/arraytraits.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,46 @@ impl<S, D, I> IndexMut<I> for ArrayBase<S, D>
9494
}
9595
}
9696

97+
impl<'a, A, D> ArrayView<'a, A, D>
98+
where
99+
D: Dimension,
100+
{
101+
/// Get a reference of a element through the view.
102+
///
103+
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
104+
/// https://github.com/bluss/rust-ndarray/issues/371
105+
pub fn elem_ref<I: NdIndex<D>>(&self, index: I) -> &'a A {
106+
debug_bounds_check!(self, index);
107+
unsafe {
108+
&*self.as_ptr().offset(
109+
index
110+
.index_checked(&self.dim, &self.strides)
111+
.unwrap_or_else(|| array_out_of_bounds()),
112+
)
113+
}
114+
}
115+
}
116+
117+
impl<'a, A, D> ArrayViewMut<'a, A, D>
118+
where
119+
D: Dimension,
120+
{
121+
/// Get a mutable reference of a element through the view.
122+
///
123+
/// This is a replacement of `IndexMut::index_mut` since it forces us a wrong lifetime
124+
/// https://github.com/bluss/rust-ndarray/issues/371
125+
pub fn elem_ref_mut<I: NdIndex<D>>(&mut self, index: I) -> &'a mut A {
126+
debug_bounds_check!(self, index);
127+
unsafe {
128+
&mut *self.as_mut_ptr().offset(
129+
index
130+
.index_checked(&self.dim, &self.strides)
131+
.unwrap_or_else(|| array_out_of_bounds()),
132+
)
133+
}
134+
}
135+
}
136+
97137
/// Return `true` if the array shapes and all elements of `self` and
98138
/// `rhs` are equal. Return `false` otherwise.
99139
impl<S, S2, D> PartialEq<ArrayBase<S2, D>> for ArrayBase<S, D>

0 commit comments

Comments
 (0)