Skip to content

Get a correct reference to an element from ArrayView #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ impl<S, D, I> IndexMut<I> for ArrayBase<S, D>
}
}

impl<'a, A, D> ArrayView<'a, A, D>
where
D: Dimension,
{
/// Get a reference of a element through the view.
///
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
/// https://github.com/bluss/rust-ndarray/issues/371
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can edit this doc myself. We're going to use a matter-of-fact approach, but still be positive. This is a version of index that gives out references of a longer lifetime. 😄

pub fn elem<I: NdIndex<D>>(&self, index: I) -> &'a A {
debug_bounds_check!(self, index);
unsafe {
&*self.as_ptr().offset(
index
.index_checked(&self.dim, &self.strides)
.unwrap_or_else(|| array_out_of_bounds()),
)
}
}

/// Get a reference of a element through the view without boundary check
///
/// This is a replacement of `Index::index` since it forces us a wrong lifetime
/// https://github.com/bluss/rust-ndarray/issues/371
pub fn uelem<I: NdIndex<D>>(&self, index: I) -> &'a A {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method must be unsafe. Sorry, I should not have merged without this fixed. I'll fix it now, though.

debug_bounds_check!(self, index);
unsafe { &*self.as_ptr().offset(index.index_unchecked(&self.strides)) }
}
}

impl<'a, A, D> ArrayViewMut<'a, A, D>
where
D: Dimension,
{
/// Convert a mutable array view to a mutable reference of a element.
pub fn into_elem<I: NdIndex<D>>(mut self, index: I) -> &'a mut A {
debug_bounds_check!(self, index);
unsafe {
&mut *self.as_mut_ptr().offset(
index
.index_checked(&self.dim, &self.strides)
.unwrap_or_else(|| array_out_of_bounds()),
)
}
}

/// Convert a mutable array view to a mutable reference of a element without boundary check
pub fn into_elem_unchecked<I: NdIndex<D>>(mut self, index: I) -> &'a mut A {
debug_bounds_check!(self, index);
unsafe {
&mut *self.as_mut_ptr().offset(
index.index_unchecked(&self.strides),
)
}
}
}

/// Return `true` if the array shapes and all elements of `self` and
/// `rhs` are equal. Return `false` otherwise.
impl<S, S2, D> PartialEq<ArrayBase<S2, D>> for ArrayBase<S, D>
Expand Down