Skip to content

Commit 901a998

Browse files
committed
Convert Baseiter to use NonNull throughout
Complete the transition to using NonNull as the raw pointer type by using it as Baseiter's iterator element type.
1 parent a9605dc commit 901a998

File tree

6 files changed

+42
-40
lines changed

6 files changed

+42
-40
lines changed

src/data_repr.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ impl<A> OwnedRepr<A>
5959
self.ptr.as_ptr()
6060
}
6161

62-
pub(crate) fn as_ptr_mut(&self) -> *mut A
63-
{
64-
self.ptr.as_ptr()
65-
}
66-
6762
pub(crate) fn as_nonnull_mut(&mut self) -> NonNull<A>
6863
{
6964
self.ptr

src/impl_owned_array.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[cfg(not(feature = "std"))]
22
use alloc::vec::Vec;
3+
use core::ptr::NonNull;
34
use std::mem;
45
use std::mem::MaybeUninit;
56

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

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

@@ -878,7 +880,7 @@ where D: Dimension
878880
}
879881
sort_axes_in_default_order(&mut self_);
880882
// with uninverted axes this is now the element with lowest address
881-
let array_memory_head_ptr = self_.ptr.as_ptr();
883+
let array_memory_head_ptr = self_.ptr;
882884
let data_end_ptr = data_ptr.add(data_len);
883885
debug_assert!(data_ptr <= array_memory_head_ptr);
884886
debug_assert!(array_memory_head_ptr <= data_end_ptr);
@@ -917,7 +919,7 @@ where D: Dimension
917919
// should now be dropped. This interval may be empty, then we just skip this loop.
918920
while last_ptr != elem_ptr {
919921
debug_assert!(last_ptr < data_end_ptr);
920-
std::ptr::drop_in_place(last_ptr);
922+
std::ptr::drop_in_place(last_ptr.as_mut());
921923
last_ptr = last_ptr.add(1);
922924
dropped_elements += 1;
923925
}
@@ -926,7 +928,7 @@ where D: Dimension
926928
}
927929

928930
while last_ptr < data_end_ptr {
929-
std::ptr::drop_in_place(last_ptr);
931+
std::ptr::drop_in_place(last_ptr.as_mut());
930932
last_ptr = last_ptr.add(1);
931933
dropped_elements += 1;
932934
}

src/iterators/chunks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl_iterator! {
204204

205205
fn item(&mut self, ptr) {
206206
unsafe {
207-
ArrayView::new_(
207+
ArrayView::new(
208208
ptr,
209209
self.chunk.clone(),
210210
self.inner_strides.clone())
@@ -226,7 +226,7 @@ impl_iterator! {
226226

227227
fn item(&mut self, ptr) {
228228
unsafe {
229-
ArrayViewMut::new_(
229+
ArrayViewMut::new(
230230
ptr,
231231
self.chunk.clone(),
232232
self.inner_strides.clone())

src/iterators/into_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ where D: Dimension
9191
while let Some(_) = self.next() {}
9292

9393
unsafe {
94-
let data_ptr = self.array_data.as_ptr_mut();
94+
let data_ptr = self.array_data.as_nonnull_mut();
9595
let view = RawArrayViewMut::new(self.array_head_ptr, self.inner.dim.clone(), self.inner.strides.clone());
9696
debug_assert!(self.inner.dim.size() < self.data_len, "data_len {} and dim size {}",
9797
self.data_len, self.inner.dim.size());

src/iterators/mod.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::slice::{self, Iter as SliceIter, IterMut as SliceIterMut};
3838

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

6868
impl<A, D: Dimension> Iterator for Baseiter<A, D>
6969
{
70-
type Item = *mut A;
70+
type Item = NonNull<A>;
7171

7272
#[inline]
73-
fn next(&mut self) -> Option<*mut A>
73+
fn next(&mut self) -> Option<Self::Item>
7474
{
7575
let index = match self.index {
7676
None => return None,
7777
Some(ref ix) => ix.clone(),
7878
};
7979
let offset = D::stride_offset(&index, &self.strides);
8080
self.index = self.dim.next_for(index);
81-
unsafe { Some(self.ptr.offset(offset).as_ptr()) }
81+
unsafe { Some(self.ptr.offset(offset)) }
8282
}
8383

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

9090
fn fold<Acc, G>(mut self, init: Acc, mut g: G) -> Acc
91-
where G: FnMut(Acc, *mut A) -> Acc
91+
where G: FnMut(Acc, Self::Item) -> Acc
9292
{
9393
let ndim = self.dim.ndim();
9494
debug_assert_ne!(ndim, 0);
@@ -103,7 +103,7 @@ impl<A, D: Dimension> Iterator for Baseiter<A, D>
103103
let mut i = 0;
104104
let i_end = len - elem_index;
105105
while i < i_end {
106-
accum = g(accum, row_ptr.offset(i as isize * stride).as_ptr());
106+
accum = g(accum, row_ptr.offset(i as isize * stride));
107107
i += 1;
108108
}
109109
}
@@ -137,7 +137,7 @@ impl<A, D: Dimension> ExactSizeIterator for Baseiter<A, D>
137137
impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
138138
{
139139
#[inline]
140-
fn next_back(&mut self) -> Option<*mut A>
140+
fn next_back(&mut self) -> Option<Self::Item>
141141
{
142142
let index = match self.index {
143143
None => return None,
@@ -149,10 +149,10 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
149149
self.index = None;
150150
}
151151

152-
unsafe { Some(self.ptr.offset(offset).as_ptr()) }
152+
unsafe { Some(self.ptr.offset(offset)) }
153153
}
154154

155-
fn nth_back(&mut self, n: usize) -> Option<*mut A>
155+
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
156156
{
157157
let index = self.index?;
158158
let len = self.dim[0] - index[0];
@@ -162,15 +162,15 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
162162
if index == self.dim {
163163
self.index = None;
164164
}
165-
unsafe { Some(self.ptr.offset(offset).as_ptr()) }
165+
unsafe { Some(self.ptr.offset(offset)) }
166166
} else {
167167
self.index = None;
168168
None
169169
}
170170
}
171171

172172
fn rfold<Acc, G>(mut self, init: Acc, mut g: G) -> Acc
173-
where G: FnMut(Acc, *mut A) -> Acc
173+
where G: FnMut(Acc, Self::Item) -> Acc
174174
{
175175
let mut accum = init;
176176
if let Some(index) = self.index {
@@ -182,8 +182,7 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
182182
accum = g(
183183
accum,
184184
self.ptr
185-
.offset(Ix1::stride_offset(&self.dim, &self.strides))
186-
.as_ptr(),
185+
.offset(Ix1::stride_offset(&self.dim, &self.strides)),
187186
);
188187
}
189188
}
@@ -231,7 +230,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
231230
#[inline]
232231
fn next(&mut self) -> Option<&'a A>
233232
{
234-
self.inner.next().map(|p| unsafe { &*p })
233+
self.inner.next().map(|p| unsafe { p.as_ref() })
235234
}
236235

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

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

257256
fn rfold<Acc, G>(self, init: Acc, mut g: G) -> Acc
258257
where G: FnMut(Acc, Self::Item) -> Acc
259258
{
260-
unsafe { self.inner.rfold(init, move |acc, ptr| g(acc, &*ptr)) }
259+
unsafe { self.inner.rfold(init, move |acc, ptr| g(acc, ptr.as_ref())) }
261260
}
262261
}
263262

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

657656
fn size_hint(&self) -> (usize, Option<usize>)
@@ -662,7 +661,10 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
662661
fn fold<Acc, G>(self, init: Acc, mut g: G) -> Acc
663662
where G: FnMut(Acc, Self::Item) -> Acc
664663
{
665-
unsafe { self.inner.fold(init, move |acc, ptr| g(acc, &mut *ptr)) }
664+
unsafe {
665+
self.inner
666+
.fold(init, move |acc, mut ptr| g(acc, ptr.as_mut()))
667+
}
666668
}
667669
}
668670

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

677679
fn rfold<Acc, G>(self, init: Acc, mut g: G) -> Acc
678680
where G: FnMut(Acc, Self::Item) -> Acc
679681
{
680-
unsafe { self.inner.rfold(init, move |acc, ptr| g(acc, &mut *ptr)) }
682+
unsafe {
683+
self.inner
684+
.rfold(init, move |acc, mut ptr| g(acc, ptr.as_mut()))
685+
}
681686
}
682687
}
683688

@@ -753,7 +758,7 @@ where D: Dimension
753758
{
754759
self.iter
755760
.next()
756-
.map(|ptr| unsafe { ArrayView::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
761+
.map(|ptr| unsafe { ArrayView::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
757762
}
758763

759764
fn size_hint(&self) -> (usize, Option<usize>)
@@ -777,7 +782,7 @@ impl<'a, A> DoubleEndedIterator for LanesIter<'a, A, Ix1>
777782
{
778783
self.iter
779784
.next_back()
780-
.map(|ptr| unsafe { ArrayView::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
785+
.map(|ptr| unsafe { ArrayView::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
781786
}
782787
}
783788

@@ -805,7 +810,7 @@ where D: Dimension
805810
{
806811
self.iter
807812
.next()
808-
.map(|ptr| unsafe { ArrayViewMut::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
813+
.map(|ptr| unsafe { ArrayViewMut::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
809814
}
810815

811816
fn size_hint(&self) -> (usize, Option<usize>)
@@ -829,7 +834,7 @@ impl<'a, A> DoubleEndedIterator for LanesIterMut<'a, A, Ix1>
829834
{
830835
self.iter
831836
.next_back()
832-
.map(|ptr| unsafe { ArrayViewMut::new_(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
837+
.map(|ptr| unsafe { ArrayViewMut::new(ptr, Ix1(self.inner_len), Ix1(self.inner_stride as Ix)) })
833838
}
834839
}
835840

src/iterators/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl_iterator! {
115115

116116
fn item(&mut self, ptr) {
117117
unsafe {
118-
ArrayView::new_(
118+
ArrayView::new(
119119
ptr,
120120
self.window.clone(),
121121
self.strides.clone())

0 commit comments

Comments
 (0)