Skip to content

Commit 7c42e19

Browse files
committed
Rename to elem/into_elem and add unchecked versions
1 parent 9897683 commit 7c42e19

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
@@ -102,7 +102,7 @@ where
102102
///
103103
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
104104
/// https://github.com/bluss/rust-ndarray/issues/371
105-
pub fn elem_ref<I: NdIndex<D>>(&self, index: I) -> &'a A {
105+
pub fn elem<I: NdIndex<D>>(&self, index: I) -> &'a A {
106106
debug_bounds_check!(self, index);
107107
unsafe {
108108
&*self.as_ptr().offset(
@@ -112,17 +112,23 @@ where
112112
)
113113
}
114114
}
115+
116+
/// Get a reference of a element through the view without boundary check
117+
///
118+
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
119+
/// https://github.com/bluss/rust-ndarray/issues/371
120+
pub fn uelem<I: NdIndex<D>>(&self, index: I) -> &'a A {
121+
debug_bounds_check!(self, index);
122+
unsafe { &*self.as_ptr().offset(index.index_unchecked(&self.strides)) }
123+
}
115124
}
116125

117126
impl<'a, A, D> ArrayViewMut<'a, A, D>
118127
where
119128
D: Dimension,
120129
{
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 {
130+
/// Convert a mutable array view to a mutable reference of a element.
131+
pub fn into_elem<I: NdIndex<D>>(mut self, index: I) -> &'a mut A {
126132
debug_bounds_check!(self, index);
127133
unsafe {
128134
&mut *self.as_mut_ptr().offset(
@@ -132,6 +138,16 @@ where
132138
)
133139
}
134140
}
141+
142+
/// Convert a mutable array view to a mutable reference of a element without boundary check
143+
pub fn into_elem_unchecked<I: NdIndex<D>>(mut self, index: I) -> &'a mut A {
144+
debug_bounds_check!(self, index);
145+
unsafe {
146+
&mut *self.as_mut_ptr().offset(
147+
index.index_unchecked(&self.strides),
148+
)
149+
}
150+
}
135151
}
136152

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

0 commit comments

Comments
 (0)