diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 745a8e60b..21df6c9b8 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -732,13 +732,26 @@ where /// ``` pub fn get(&self, index: I) -> Option<&A> where - I: NdIndex, S: Data, + I: NdIndex, { unsafe { self.get_ptr(index).map(|ptr| &*ptr) } } - pub(crate) fn get_ptr(&self, index: I) -> Option<*const A> + /// Return a raw pointer to the element at `index`, or return `None` + /// if the index is out of bounds. + /// + /// ``` + /// use ndarray::arr2; + /// + /// let a = arr2(&[[1., 2.], [3., 4.]]); + /// + /// let v = a.raw_view(); + /// let p = a.get_ptr((0, 1)).unwrap(); + /// + /// assert_eq!(unsafe { *p }, 2.); + /// ``` + pub fn get_ptr(&self, index: I) -> Option<*const A> where I: NdIndex, { @@ -755,10 +768,27 @@ where S: DataMut, I: NdIndex, { - unsafe { self.get_ptr_mut(index).map(|ptr| &mut *ptr) } + unsafe { self.get_mut_ptr(index).map(|ptr| &mut *ptr) } } - pub(crate) fn get_ptr_mut(&mut self, index: I) -> Option<*mut A> + /// Return a raw pointer to the element at `index`, or return `None` + /// if the index is out of bounds. + /// + /// ``` + /// use ndarray::arr2; + /// + /// let mut a = arr2(&[[1., 2.], [3., 4.]]); + /// + /// let v = a.raw_view_mut(); + /// let p = a.get_mut_ptr((0, 1)).unwrap(); + /// + /// unsafe { + /// *p = 5.; + /// } + /// + /// assert_eq!(a.get((0, 1)), Some(&5.)); + /// ``` + pub fn get_mut_ptr(&mut self, index: I) -> Option<*mut A> where S: RawDataMut, I: NdIndex, diff --git a/src/impl_views/indexing.rs b/src/impl_views/indexing.rs index c73e870e6..adb911a48 100644 --- a/src/impl_views/indexing.rs +++ b/src/impl_views/indexing.rs @@ -164,7 +164,7 @@ where fn index(mut self, index: I) -> &'a mut A { debug_bounds_check!(self, index); unsafe { - match self.get_ptr_mut(index) { + match self.get_mut_ptr(index) { Some(ptr) => &mut *ptr, None => array_out_of_bounds(), } @@ -182,7 +182,7 @@ where fn get(mut self, index: I) -> Option<&'a mut A> { debug_bounds_check!(self, index); unsafe { - match self.get_ptr_mut(index) { + match self.get_mut_ptr(index) { Some(ptr) => Some(&mut *ptr), None => None, }