Skip to content

Change ArrayBase.ptr to NonNull type #683

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 8 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
fn index(&self, index: I) -> &S::Elem {
debug_bounds_check!(self, index);
unsafe {
&*self.ptr.offset(
&*self.ptr.as_ptr().offset(
index
.index_checked(&self.dim, &self.strides)
.unwrap_or_else(|| array_out_of_bounds()),
Expand Down
40 changes: 21 additions & 19 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! The data (inner representation) traits for ndarray

use std::mem::{self, size_of};
use std::ptr::NonNull;
use std::sync::Arc;

use crate::{
Expand Down Expand Up @@ -67,14 +68,14 @@ pub unsafe trait RawDataMut: RawData {
pub unsafe trait RawDataClone: RawData {
#[doc(hidden)]
/// Unsafe because, `ptr` must point inside the current storage.
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem);
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>);

#[doc(hidden)]
unsafe fn clone_from_with_ptr(
&mut self,
other: &Self,
ptr: *mut Self::Elem,
) -> *mut Self::Elem {
ptr: NonNull<Self::Elem>,
) -> NonNull<Self::Elem> {
let (data, ptr) = other.clone_with_ptr(ptr);
*self = data;
ptr
Expand Down Expand Up @@ -148,7 +149,7 @@ unsafe impl<A> RawData for RawViewRepr<*const A> {
}

unsafe impl<A> RawDataClone for RawViewRepr<*const A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>) {
(*self, ptr)
}
}
Expand Down Expand Up @@ -177,7 +178,7 @@ unsafe impl<A> RawDataMut for RawViewRepr<*mut A> {
}

unsafe impl<A> RawDataClone for RawViewRepr<*mut A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>) {
(*self, ptr)
}
}
Expand Down Expand Up @@ -217,13 +218,13 @@ where
let rcvec = &mut self_.data.0;
let a_size = mem::size_of::<A>() as isize;
let our_off = if a_size != 0 {
(self_.ptr as isize - rcvec.as_ptr() as isize) / a_size
(self_.ptr.as_ptr() as isize - rcvec.as_ptr() as isize) / a_size
} else {
0
};
let rvec = Arc::make_mut(rcvec);
unsafe {
self_.ptr = rvec.as_mut_ptr().offset(our_off);
self_.ptr = NonNull::new(rvec.as_mut_ptr().offset(our_off)).unwrap();
}
}

Expand Down Expand Up @@ -252,7 +253,7 @@ unsafe impl<A> Data for OwnedArcRepr<A> {
unsafe impl<A> DataMut for OwnedArcRepr<A> where A: Clone {}

unsafe impl<A> RawDataClone for OwnedArcRepr<A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>) {
// pointer is preserved
(self.clone(), ptr)
}
Expand Down Expand Up @@ -298,28 +299,29 @@ unsafe impl<A> RawDataClone for OwnedRepr<A>
where
A: Clone,
{
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>) {
let mut u = self.clone();
let mut new_ptr = u.0.as_mut_ptr();
if size_of::<A>() != 0 {
let our_off = (ptr as isize - self.0.as_ptr() as isize) / mem::size_of::<A>() as isize;
let our_off =
(ptr.as_ptr() as isize - self.0.as_ptr() as isize) / mem::size_of::<A>() as isize;
new_ptr = new_ptr.offset(our_off);
}
(u, new_ptr)
(u, NonNull::new(new_ptr).unwrap())
}

unsafe fn clone_from_with_ptr(
&mut self,
other: &Self,
ptr: *mut Self::Elem,
) -> *mut Self::Elem {
ptr: NonNull<Self::Elem>,
) -> NonNull<Self::Elem> {
let our_off = if size_of::<A>() != 0 {
(ptr as isize - other.0.as_ptr() as isize) / mem::size_of::<A>() as isize
(ptr.as_ptr() as isize - other.0.as_ptr() as isize) / mem::size_of::<A>() as isize
} else {
0
};
self.0.clone_from(&other.0);
self.0.as_mut_ptr().offset(our_off)
NonNull::new(self.0.as_mut_ptr().offset(our_off)).unwrap()
}
}

Expand All @@ -342,7 +344,7 @@ unsafe impl<'a, A> Data for ViewRepr<&'a A> {
}

unsafe impl<'a, A> RawDataClone for ViewRepr<&'a A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>) {
(*self, ptr)
}
}
Expand Down Expand Up @@ -469,7 +471,7 @@ unsafe impl<'a, A> RawDataClone for CowRepr<'a, A>
where
A: Clone,
{
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
unsafe fn clone_with_ptr(&self, ptr: NonNull<Self::Elem>) -> (Self, NonNull<Self::Elem>) {
match self {
CowRepr::View(view) => {
let (new_view, ptr) = view.clone_with_ptr(ptr);
Expand All @@ -486,8 +488,8 @@ where
unsafe fn clone_from_with_ptr(
&mut self,
other: &Self,
ptr: *mut Self::Elem,
) -> *mut Self::Elem {
ptr: NonNull<Self::Elem>,
) -> NonNull<Self::Elem> {
match (&mut *self, other) {
(CowRepr::View(self_), CowRepr::View(other)) => self_.clone_from_with_ptr(other, ptr),
(CowRepr::Owned(self_), CowRepr::Owned(other)) => self_.clone_from_with_ptr(other, ptr),
Expand Down
1 change: 1 addition & 0 deletions src/impl_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::imp_prelude::*;
use crate::RawDataClone;

Expand Down
4 changes: 3 additions & 1 deletion src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#![allow(clippy::match_wild_err_arm)]

use std::ptr::NonNull;

use num_traits::{Float, One, Zero};

use crate::dimension;
Expand Down Expand Up @@ -422,7 +424,7 @@ where
// debug check for issues that indicates wrong use of this constructor
debug_assert!(dimension::can_index_slice(&v, &dim, &strides).is_ok());
ArrayBase {
ptr: v.as_mut_ptr(),
ptr: NonNull::new(v.as_mut_ptr()).unwrap(),
data: DataOwned::new(v),
strides,
dim,
Expand Down
43 changes: 24 additions & 19 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use std::cmp;
use std::ptr as std_ptr;
use std::slice;
use std_ptr::NonNull;

use itertools::{izip, zip};

Expand Down Expand Up @@ -138,7 +139,7 @@ where
S: Data,
{
debug_assert!(self.pointer_is_inbounds());
unsafe { ArrayView::new_(self.ptr, self.dim.clone(), self.strides.clone()) }
unsafe { ArrayView::new_(self.ptr.as_ptr(), self.dim.clone(), self.strides.clone()) }
}

/// Return a read-write view of the array
Expand All @@ -147,7 +148,7 @@ where
S: DataMut,
{
self.ensure_unique();
unsafe { ArrayViewMut::new_(self.ptr, self.dim.clone(), self.strides.clone()) }
unsafe { ArrayViewMut::new_(self.ptr.as_ptr(), self.dim.clone(), self.strides.clone()) }
}

/// Return an uniquely owned copy of the array.
Expand Down Expand Up @@ -468,7 +469,7 @@ where
indices,
);
unsafe {
self.ptr = self.ptr.offset(offset);
self.ptr = NonNull::new(self.ptr.as_ptr().offset(offset)).unwrap();
}
debug_assert!(self.pointer_is_inbounds());
}
Expand Down Expand Up @@ -506,7 +507,7 @@ where
let ptr = self.ptr;
index
.index_checked(&self.dim, &self.strides)
.map(move |offset| unsafe { ptr.offset(offset) as *const _ })
.map(move |offset| unsafe { ptr.as_ptr().offset(offset) as *const _ })
}

/// Return a mutable reference to the element at `index`, or return `None`
Expand Down Expand Up @@ -545,7 +546,7 @@ where
{
arraytraits::debug_bounds_check(self, &index);
let off = index.index_unchecked(&self.strides);
&*self.ptr.offset(off)
&*self.ptr.as_ptr().offset(off)
}

/// Perform *unchecked* array indexing.
Expand All @@ -563,7 +564,7 @@ where
debug_assert!(self.data.is_unique());
arraytraits::debug_bounds_check(self, &index);
let off = index.index_unchecked(&self.strides);
&mut *self.ptr.offset(off)
&mut *self.ptr.as_ptr().offset(off)
}

/// Swap elements at indices `index1` and `index2`.
Expand Down Expand Up @@ -599,7 +600,10 @@ where
arraytraits::debug_bounds_check(self, &index2);
let off1 = index1.index_unchecked(&self.strides);
let off2 = index2.index_unchecked(&self.strides);
std_ptr::swap(self.ptr.offset(off1), self.ptr.offset(off2));
std_ptr::swap(
self.ptr.as_ptr().offset(off1),
self.ptr.as_ptr().offset(off2),
);
}

// `get` for zero-dimensional arrays
Expand Down Expand Up @@ -698,7 +702,7 @@ where
/// **Panics** if `axis` or `index` is out of bounds.
pub fn collapse_axis(&mut self, axis: Axis, index: usize) {
let offset = dimension::do_collapse_axis(&mut self.dim, &self.strides, axis.index(), index);
self.ptr = unsafe { self.ptr.offset(offset) };
self.ptr = unsafe { NonNull::new(self.ptr.as_ptr().offset(offset)).unwrap() };
debug_assert!(self.pointer_is_inbounds());
}

Expand Down Expand Up @@ -1271,7 +1275,7 @@ where
/// where *d* is `self.ndim()`.
#[inline(always)]
pub fn as_ptr(&self) -> *const A {
self.ptr
self.ptr.as_ptr() as *const A
}

/// Return a mutable pointer to the first element in the array.
Expand All @@ -1281,13 +1285,13 @@ where
S: RawDataMut,
{
self.try_ensure_unique(); // for RcArray
self.ptr
self.ptr.as_ptr()
}

/// Return a raw view of the array.
#[inline]
pub fn raw_view(&self) -> RawArrayView<A, D> {
unsafe { RawArrayView::new_(self.ptr, self.dim.clone(), self.strides.clone()) }
unsafe { RawArrayView::new_(self.ptr.as_ptr(), self.dim.clone(), self.strides.clone()) }
}

/// Return a raw mutable view of the array.
Expand All @@ -1297,7 +1301,7 @@ where
S: RawDataMut,
{
self.try_ensure_unique(); // for RcArray
unsafe { RawArrayViewMut::new_(self.ptr, self.dim.clone(), self.strides.clone()) }
unsafe { RawArrayViewMut::new_(self.ptr.as_ptr(), self.dim.clone(), self.strides.clone()) }
}

/// Return the array’s data as a slice, if it is contiguous and in standard order.
Expand All @@ -1310,7 +1314,7 @@ where
S: Data,
{
if self.is_standard_layout() {
unsafe { Some(slice::from_raw_parts(self.ptr, self.len())) }
unsafe { Some(slice::from_raw_parts(self.ptr.as_ptr(), self.len())) }
} else {
None
}
Expand All @@ -1324,7 +1328,7 @@ where
{
if self.is_standard_layout() {
self.ensure_unique();
unsafe { Some(slice::from_raw_parts_mut(self.ptr, self.len())) }
unsafe { Some(slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len())) }
} else {
None
}
Expand All @@ -1342,7 +1346,7 @@ where
S: Data,
{
if self.is_contiguous() {
unsafe { Some(slice::from_raw_parts(self.ptr, self.len())) }
unsafe { Some(slice::from_raw_parts(self.ptr.as_ptr(), self.len())) }
} else {
None
}
Expand All @@ -1356,7 +1360,7 @@ where
{
if self.is_contiguous() {
self.ensure_unique();
unsafe { Some(slice::from_raw_parts_mut(self.ptr, self.len())) }
unsafe { Some(slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len())) }
} else {
None
}
Expand Down Expand Up @@ -1594,7 +1598,7 @@ where
Some(st) => st,
None => return None,
};
unsafe { Some(ArrayView::new_(self.ptr, dim, broadcast_strides)) }
unsafe { Some(ArrayView::new_(self.ptr.as_ptr(), dim, broadcast_strides)) }
}

/// Swap axes `ax` and `bx`.
Expand Down Expand Up @@ -1719,7 +1723,8 @@ where
let s = self.strides.axis(axis) as Ixs;
let m = self.dim.axis(axis);
if m != 0 {
self.ptr = self.ptr.offset(stride_offset(m - 1, s as Ix));
self.ptr =
NonNull::new(self.ptr.as_ptr().offset(stride_offset(m - 1, s as Ix))).unwrap();
}
self.strides.set_axis(axis, (-s) as Ix);
}
Expand Down Expand Up @@ -1821,7 +1826,7 @@ where
Some(slc) => {
let ptr = slc.as_ptr() as *mut A;
let end = unsafe { ptr.add(slc.len()) };
self.ptr >= ptr && self.ptr <= end
self.ptr.as_ptr() >= ptr && self.ptr.as_ptr() <= end
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/impl_owned_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<A> Array<A, Ix0> {
// (This is necessary because the element in the array might not be
// the first element in the `Vec`, such as if the array was created
// by `array![1, 2, 3, 4].slice_move(s![2])`.)
let first = self.ptr as usize;
let first = self.ptr.as_ptr() as usize;
let base = self.data.0.as_ptr() as usize;
let index = (first - base) / size;
debug_assert_eq!((first - base) % size, 0);
Expand Down
Loading