Skip to content

Commit

Permalink
Make ArrayBase::get_ptr(_mut) public to enable indexing into raw views.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold authored and jturner314 committed Jul 30, 2022
1 parent dedb15f commit 2f0b37b
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,26 @@ where
/// ```
pub fn get<I>(&self, index: I) -> Option<&A>
where
I: NdIndex<D>,
S: Data,
I: NdIndex<D>,
{
unsafe { self.get_ptr(index).map(|ptr| &*ptr) }
}

pub(crate) fn get_ptr<I>(&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<I>(&self, index: I) -> Option<*const A>
where
I: NdIndex<D>,
{
Expand All @@ -758,7 +771,24 @@ where
unsafe { self.get_ptr_mut(index).map(|ptr| &mut *ptr) }
}

pub(crate) fn get_ptr_mut<I>(&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<I>(&mut self, index: I) -> Option<*mut A>
where
S: RawDataMut,
I: NdIndex<D>,
Expand Down

0 comments on commit 2f0b37b

Please sign in to comment.