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 3 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
5 changes: 3 additions & 2 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 @@ -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
12 changes: 9 additions & 3 deletions src/impl_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
// <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 std::ptr::NonNull;

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

impl<S: RawDataClone, D: Clone> Clone for ArrayBase<S, D> {
fn clone(&self) -> ArrayBase<S, D> {
unsafe {
let (data, ptr) = self.data.clone_with_ptr(self.ptr);
let (data, ptr) = self.data.clone_with_ptr(self.ptr.as_ptr());
ArrayBase {
data,
ptr,
ptr: NonNull::new(ptr).unwrap(),
dim: self.dim.clone(),
strides: self.strides.clone(),
}
Expand All @@ -26,7 +28,11 @@ impl<S: RawDataClone, D: Clone> Clone for ArrayBase<S, D> {
/// potentially more efficient.
fn clone_from(&mut self, other: &Self) {
unsafe {
self.ptr = self.data.clone_from_with_ptr(&other.data, other.ptr);
self.ptr = NonNull::new(
self.data
.clone_from_with_ptr(&other.data, other.ptr.as_ptr()),
)
.unwrap();
self.dim.clone_from(&other.dim);
self.strides.clone_from(&other.strides);
}
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
24 changes: 13 additions & 11 deletions src/impl_raw_views.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ptr::NonNull;

use crate::dimension::{self, stride_offset};
use crate::imp_prelude::*;
use crate::{is_aligned, StrideShape};
Expand All @@ -14,7 +16,7 @@ where
pub(crate) unsafe fn new_(ptr: *const A, dim: D, strides: D) -> Self {
RawArrayView {
data: RawViewRepr::new(),
ptr: ptr as *mut A,
ptr: NonNull::new(ptr as *mut A).unwrap(),
dim,
strides,
}
Expand Down Expand Up @@ -75,7 +77,7 @@ where
/// ensure that all of the data is valid and choose the correct lifetime.
#[inline]
pub unsafe fn deref_into_view<'a>(self) -> ArrayView<'a, A, D> {
ArrayView::new_(self.ptr, self.dim, self.strides)
ArrayView::new_(self.ptr.as_ptr(), self.dim, self.strides)
}

/// Split the array view along `axis` and return one array pointer strictly
Expand All @@ -84,13 +86,13 @@ where
/// **Panics** if `axis` or `index` is out of bounds.
pub fn split_at(self, axis: Axis, index: Ix) -> (Self, Self) {
assert!(index <= self.len_of(axis));
let left_ptr = self.ptr;
let left_ptr = self.ptr.as_ptr();
let right_ptr = if index == self.len_of(axis) {
self.ptr
self.ptr.as_ptr()
} else {
let offset = stride_offset(index, self.strides.axis(axis));
// The `.offset()` is safe due to the guarantees of `RawData`.
unsafe { self.ptr.offset(offset) }
unsafe { self.ptr.as_ptr().offset(offset) }
};

let mut dim_left = self.dim.clone();
Expand Down Expand Up @@ -118,7 +120,7 @@ where
pub(crate) unsafe fn new_(ptr: *mut A, dim: D, strides: D) -> Self {
RawArrayViewMut {
data: RawViewRepr::new(),
ptr,
ptr: NonNull::new(ptr).unwrap(),
dim,
strides,
}
Expand Down Expand Up @@ -175,7 +177,7 @@ where
/// Converts to a non-mutable `RawArrayView`.
#[inline]
pub(crate) fn into_raw_view(self) -> RawArrayView<A, D> {
unsafe { RawArrayView::new_(self.ptr, self.dim, self.strides) }
unsafe { RawArrayView::new_(self.ptr.as_ptr(), self.dim, self.strides) }
}

/// Converts to a read-only view of the array.
Expand All @@ -185,7 +187,7 @@ where
/// ensure that all of the data is valid and choose the correct lifetime.
#[inline]
pub unsafe fn deref_into_view<'a>(self) -> ArrayView<'a, A, D> {
ArrayView::new_(self.ptr, self.dim, self.strides)
ArrayView::new_(self.ptr.as_ptr(), self.dim, self.strides)
}

/// Converts to a mutable view of the array.
Expand All @@ -195,7 +197,7 @@ where
/// ensure that all of the data is valid and choose the correct lifetime.
#[inline]
pub unsafe fn deref_into_view_mut<'a>(self) -> ArrayViewMut<'a, A, D> {
ArrayViewMut::new_(self.ptr, self.dim, self.strides)
ArrayViewMut::new_(self.ptr.as_ptr(), self.dim, self.strides)
}

/// Split the array view along `axis` and return one array pointer strictly
Expand All @@ -206,8 +208,8 @@ where
let (left, right) = self.into_raw_view().split_at(axis, index);
unsafe {
(
Self::new_(left.ptr, left.dim, left.strides),
Self::new_(right.ptr, right.dim, right.strides),
Self::new_(left.ptr.as_ptr(), left.dim, left.strides),
Self::new_(right.ptr.as_ptr(), right.dim, right.strides),
)
}
}
Expand Down
Loading