Skip to content

Commit

Permalink
Convert Baseiter to use NonNull throughout
Browse files Browse the repository at this point in the history
Complete the transition to using NonNull as the raw pointer type by
using it as Baseiter's iterator element type.
  • Loading branch information
bluss committed Aug 6, 2024
1 parent a9605dc commit 00e1546
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
5 changes: 0 additions & 5 deletions src/data_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ impl<A> OwnedRepr<A>
self.ptr.as_ptr()
}

pub(crate) fn as_ptr_mut(&self) -> *mut A
{
self.ptr.as_ptr()
}

pub(crate) fn as_nonnull_mut(&mut self) -> NonNull<A>
{
self.ptr
Expand Down
14 changes: 8 additions & 6 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::ptr::NonNull;
use std::mem;
use std::mem::MaybeUninit;

Expand Down Expand Up @@ -435,7 +436,7 @@ where D: Dimension
// "deconstruct" self; the owned repr releases ownership of all elements and we
// carry on with raw view methods
let data_len = self.data.len();
let data_ptr = self.data.as_nonnull_mut().as_ptr();
let data_ptr = self.data.as_nonnull_mut();

unsafe {
// Safety: self.data releases ownership of the elements. Any panics below this point
Expand Down Expand Up @@ -866,8 +867,9 @@ where D: Dimension
///
/// This is an internal function for use by move_into and IntoIter only, safety invariants may need
/// to be upheld across the calls from those implementations.
pub(crate) unsafe fn drop_unreachable_raw<A, D>(mut self_: RawArrayViewMut<A, D>, data_ptr: *mut A, data_len: usize)
where D: Dimension
pub(crate) unsafe fn drop_unreachable_raw<A, D>(
mut self_: RawArrayViewMut<A, D>, data_ptr: NonNull<A>, data_len: usize,
) where D: Dimension
{
let self_len = self_.len();

Expand All @@ -878,7 +880,7 @@ where D: Dimension
}
sort_axes_in_default_order(&mut self_);
// with uninverted axes this is now the element with lowest address
let array_memory_head_ptr = self_.ptr.as_ptr();
let array_memory_head_ptr = self_.ptr;
let data_end_ptr = data_ptr.add(data_len);
debug_assert!(data_ptr <= array_memory_head_ptr);
debug_assert!(array_memory_head_ptr <= data_end_ptr);
Expand Down Expand Up @@ -917,7 +919,7 @@ where D: Dimension
// should now be dropped. This interval may be empty, then we just skip this loop.
while last_ptr != elem_ptr {
debug_assert!(last_ptr < data_end_ptr);
std::ptr::drop_in_place(last_ptr);
std::ptr::drop_in_place(last_ptr.as_mut());
last_ptr = last_ptr.add(1);
dropped_elements += 1;
}
Expand All @@ -926,7 +928,7 @@ where D: Dimension
}

while last_ptr < data_end_ptr {
std::ptr::drop_in_place(last_ptr);
std::ptr::drop_in_place(last_ptr.as_mut());
last_ptr = last_ptr.add(1);
dropped_elements += 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/iterators/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl_iterator! {

fn item(&mut self, ptr) {
unsafe {
ArrayView::new_(
ArrayView::new(
ptr,
self.chunk.clone(),
self.inner_strides.clone())
Expand All @@ -226,7 +226,7 @@ impl_iterator! {

fn item(&mut self, ptr) {
unsafe {
ArrayViewMut::new_(
ArrayViewMut::new(
ptr,
self.chunk.clone(),
self.inner_strides.clone())
Expand Down
4 changes: 2 additions & 2 deletions src/iterators/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<A, D: Dimension> Iterator for IntoIter<A, D>
#[inline]
fn next(&mut self) -> Option<A>
{
self.inner.next().map(|p| unsafe { p.read() })
self.inner.next().map(|p| unsafe { p.as_ptr().read() })
}

fn size_hint(&self) -> (usize, Option<usize>)
Expand Down Expand Up @@ -91,7 +91,7 @@ where D: Dimension
while let Some(_) = self.next() {}

unsafe {
let data_ptr = self.array_data.as_ptr_mut();
let data_ptr = self.array_data.as_nonnull_mut();
let view = RawArrayViewMut::new(self.array_head_ptr, self.inner.dim.clone(), self.inner.strides.clone());
debug_assert!(self.inner.dim.size() < self.data_len, "data_len {} and dim size {}",
self.data_len, self.inner.dim.size());
Expand Down
55 changes: 30 additions & 25 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::slice::{self, Iter as SliceIter, IterMut as SliceIterMut};

/// Base for iterators over all axes.
///
/// Iterator element type is `*mut A`.
/// Iterator element type is `NonNull<A>`.
#[derive(Debug)]
pub struct Baseiter<A, D>
{
Expand Down Expand Up @@ -67,18 +67,18 @@ impl<A, D: Dimension> Baseiter<A, D>

impl<A, D: Dimension> Iterator for Baseiter<A, D>
{
type Item = *mut A;
type Item = NonNull<A>;

#[inline]
fn next(&mut self) -> Option<*mut A>
fn next(&mut self) -> Option<Self::Item>
{
let index = match self.index {
None => return None,
Some(ref ix) => ix.clone(),
};
let offset = D::stride_offset(&index, &self.strides);
self.index = self.dim.next_for(index);
unsafe { Some(self.ptr.offset(offset).as_ptr()) }
unsafe { Some(self.ptr.offset(offset)) }
}

fn size_hint(&self) -> (usize, Option<usize>)
Expand All @@ -88,7 +88,7 @@ impl<A, D: Dimension> Iterator for Baseiter<A, D>
}

fn fold<Acc, G>(mut self, init: Acc, mut g: G) -> Acc
where G: FnMut(Acc, *mut A) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc
{
let ndim = self.dim.ndim();
debug_assert_ne!(ndim, 0);
Expand All @@ -103,7 +103,7 @@ impl<A, D: Dimension> Iterator for Baseiter<A, D>
let mut i = 0;
let i_end = len - elem_index;
while i < i_end {
accum = g(accum, row_ptr.offset(i as isize * stride).as_ptr());
accum = g(accum, row_ptr.offset(i as isize * stride));
i += 1;
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<A, D: Dimension> ExactSizeIterator for Baseiter<A, D>
impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
{
#[inline]
fn next_back(&mut self) -> Option<*mut A>
fn next_back(&mut self) -> Option<Self::Item>
{
let index = match self.index {
None => return None,
Expand All @@ -149,10 +149,10 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
self.index = None;
}

unsafe { Some(self.ptr.offset(offset).as_ptr()) }
unsafe { Some(self.ptr.offset(offset)) }
}

fn nth_back(&mut self, n: usize) -> Option<*mut A>
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
{
let index = self.index?;
let len = self.dim[0] - index[0];
Expand All @@ -162,15 +162,15 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
if index == self.dim {
self.index = None;
}
unsafe { Some(self.ptr.offset(offset).as_ptr()) }
unsafe { Some(self.ptr.offset(offset)) }
} else {
self.index = None;
None
}
}

fn rfold<Acc, G>(mut self, init: Acc, mut g: G) -> Acc
where G: FnMut(Acc, *mut A) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc
{
let mut accum = init;
if let Some(index) = self.index {
Expand All @@ -182,8 +182,7 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
accum = g(
accum,
self.ptr
.offset(Ix1::stride_offset(&self.dim, &self.strides))
.as_ptr(),
.offset(Ix1::stride_offset(&self.dim, &self.strides)),
);
}
}
Expand Down Expand Up @@ -231,7 +230,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
#[inline]
fn next(&mut self) -> Option<&'a A>
{
self.inner.next().map(|p| unsafe { &*p })
self.inner.next().map(|p| unsafe { p.as_ref() })
}

fn size_hint(&self) -> (usize, Option<usize>)
Expand All @@ -242,7 +241,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
fn fold<Acc, G>(self, init: Acc, mut g: G) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc
{
unsafe { self.inner.fold(init, move |acc, ptr| g(acc, &*ptr)) }
unsafe { self.inner.fold(init, move |acc, ptr| g(acc, ptr.as_ref())) }
}
}

Expand All @@ -251,13 +250,13 @@ impl<'a, A> DoubleEndedIterator for ElementsBase<'a, A, Ix1>
#[inline]
fn next_back(&mut self) -> Option<&'a A>
{
self.inner.next_back().map(|p| unsafe { &*p })
self.inner.next_back().map(|p| unsafe { p.as_ref() })
}

fn rfold<Acc, G>(self, init: Acc, mut g: G) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc
{
unsafe { self.inner.rfold(init, move |acc, ptr| g(acc, &*ptr)) }
unsafe { self.inner.rfold(init, move |acc, ptr| g(acc, ptr.as_ref())) }
}
}

Expand Down Expand Up @@ -651,7 +650,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
#[inline]
fn next(&mut self) -> Option<&'a mut A>
{
self.inner.next().map(|p| unsafe { &mut *p })
self.inner.next().map(|mut p| unsafe { p.as_mut() })
}

fn size_hint(&self) -> (usize, Option<usize>)
Expand All @@ -662,7 +661,10 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
fn fold<Acc, G>(self, init: Acc, mut g: G) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc
{
unsafe { self.inner.fold(init, move |acc, ptr| g(acc, &mut *ptr)) }
unsafe {
self.inner
.fold(init, move |acc, mut ptr| g(acc, ptr.as_mut()))
}
}
}

Expand All @@ -671,13 +673,16 @@ impl<'a, A> DoubleEndedIterator for ElementsBaseMut<'a, A, Ix1>
#[inline]
fn next_back(&mut self) -> Option<&'a mut A>
{
self.inner.next_back().map(|p| unsafe { &mut *p })
self.inner.next_back().map(|mut p| unsafe { p.as_mut() })
}

fn rfold<Acc, G>(self, init: Acc, mut g: G) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc
{
unsafe { self.inner.rfold(init, move |acc, ptr| g(acc, &mut *ptr)) }
unsafe {
self.inner
.rfold(init, move |acc, mut ptr| g(acc, ptr.as_mut()))
}
}
}

Expand Down Expand Up @@ -753,7 +758,7 @@ where D: Dimension
{
self.iter
.next()
.map(|ptr| unsafe { ArrayView::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
.map(|ptr| unsafe { ArrayView::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
}

fn size_hint(&self) -> (usize, Option<usize>)
Expand All @@ -777,7 +782,7 @@ impl<'a, A> DoubleEndedIterator for LanesIter<'a, A, Ix1>
{
self.iter
.next_back()
.map(|ptr| unsafe { ArrayView::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
.map(|ptr| unsafe { ArrayView::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
}
}

Expand Down Expand Up @@ -805,7 +810,7 @@ where D: Dimension
{
self.iter
.next()
.map(|ptr| unsafe { ArrayViewMut::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
.map(|ptr| unsafe { ArrayViewMut::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
}

fn size_hint(&self) -> (usize, Option<usize>)
Expand All @@ -829,7 +834,7 @@ impl<'a, A> DoubleEndedIterator for LanesIterMut<'a, A, Ix1>
{
self.iter
.next_back()
.map(|ptr| unsafe { ArrayViewMut::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
.map(|ptr| unsafe { ArrayViewMut::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/iterators/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl_iterator! {

fn item(&mut self, ptr) {
unsafe {
ArrayView::new_(
ArrayView::new(
ptr,
self.window.clone(),
self.strides.clone())
Expand Down

0 comments on commit 00e1546

Please sign in to comment.