Skip to content

Commit a3232f0

Browse files
committed
Rename to elem/into_elem and add unchecked versions
1 parent 0f0e654 commit a3232f0

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/arraytraits.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ where
103103
///
104104
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
105105
/// https://github.com/bluss/rust-ndarray/issues/371
106-
pub fn elem_ref<I: NdIndex<D>>(&self, index: I) -> &'a A {
106+
pub fn elem<I: NdIndex<D>>(&self, index: I) -> &'a A {
107107
debug_bounds_check!(self, index);
108108
unsafe {
109109
&*self.as_ptr().offset(
@@ -113,17 +113,23 @@ where
113113
)
114114
}
115115
}
116+
117+
/// Get a reference of a element through the view without boundary check
118+
///
119+
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
120+
/// https://github.com/bluss/rust-ndarray/issues/371
121+
pub fn uelem<I: NdIndex<D>>(&self, index: I) -> &'a A {
122+
debug_bounds_check!(self, index);
123+
unsafe { &*self.as_ptr().offset(index.index_unchecked(&self.strides)) }
124+
}
116125
}
117126

118127
impl<'a, A, D> ArrayViewMut<'a, A, D>
119128
where
120129
D: Dimension,
121130
{
122-
/// Get a mutable reference of a element through the view.
123-
///
124-
/// This is a replacement of `IndexMut::index_mut` since it forces us a wrong lifetime
125-
/// https://github.com/bluss/rust-ndarray/issues/371
126-
pub fn elem_ref_mut<I: NdIndex<D>>(&mut self, index: I) -> &'a mut A {
131+
/// Convert a mutable array view to a mutable reference of a element.
132+
pub fn into_elem<I: NdIndex<D>>(mut self, index: I) -> &'a mut A {
127133
debug_bounds_check!(self, index);
128134
unsafe {
129135
&mut *self.as_mut_ptr().offset(
@@ -133,6 +139,16 @@ where
133139
)
134140
}
135141
}
142+
143+
/// Convert a mutable array view to a mutable reference of a element without boundary check
144+
pub fn into_elem_unchecked<I: NdIndex<D>>(mut self, index: I) -> &'a mut A {
145+
debug_bounds_check!(self, index);
146+
unsafe {
147+
&mut *self.as_mut_ptr().offset(
148+
index.index_unchecked(&self.strides),
149+
)
150+
}
151+
}
136152
}
137153

138154
/// Return `true` if the array shapes and all elements of `self` and

0 commit comments

Comments
 (0)