From 2f0b37b7113262a3e613300dcdeca5d5fb4c023e Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sat, 22 Jan 2022 11:31:34 +0100 Subject: [PATCH 1/2] Make ArrayBase::get_ptr(_mut) public to enable indexing into raw views. --- src/impl_methods.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 745a8e60b..59b3e4a73 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, { @@ -758,7 +771,24 @@ where unsafe { self.get_ptr_mut(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_ptr_mut((0, 1)).unwrap(); + /// + /// unsafe { + /// *p = 5.; + /// } + /// + /// assert_eq!(a.get((0, 1)), Some(&5.)); + /// ``` + pub fn get_ptr_mut(&mut self, index: I) -> Option<*mut A> where S: RawDataMut, I: NdIndex, From eb506634de4f49f8c1686fd1965e408655a2c6b1 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sun, 23 Jan 2022 09:43:40 +0100 Subject: [PATCH 2/2] Rename get_ptr_mut to get_mut_ptr to be more consistent with the as_mut_ptr method. --- src/impl_methods.rs | 6 +++--- src/impl_views/indexing.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 59b3e4a73..21df6c9b8 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -768,7 +768,7 @@ 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) } } /// Return a raw pointer to the element at `index`, or return `None` @@ -780,7 +780,7 @@ where /// let mut a = arr2(&[[1., 2.], [3., 4.]]); /// /// let v = a.raw_view_mut(); - /// let p = a.get_ptr_mut((0, 1)).unwrap(); + /// let p = a.get_mut_ptr((0, 1)).unwrap(); /// /// unsafe { /// *p = 5.; @@ -788,7 +788,7 @@ where /// /// assert_eq!(a.get((0, 1)), Some(&5.)); /// ``` - pub fn get_ptr_mut(&mut self, index: I) -> Option<*mut A> + 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, }