From 2f0b37b7113262a3e613300dcdeca5d5fb4c023e Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sat, 22 Jan 2022 11:31:34 +0100 Subject: [PATCH] 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,