@@ -19,6 +19,10 @@ use alloc::vec::Vec;
19
19
use std:: iter:: FromIterator ;
20
20
use std:: marker:: PhantomData ;
21
21
use std:: ptr;
22
+ use std:: ptr:: NonNull ;
23
+
24
+ #[ allow( unused_imports) ] // Needed for Rust 1.64
25
+ use rawpointer:: PointerExt ;
22
26
23
27
use crate :: Ix1 ;
24
28
@@ -34,11 +38,11 @@ use std::slice::{self, Iter as SliceIter, IterMut as SliceIterMut};
34
38
35
39
/// Base for iterators over all axes.
36
40
///
37
- /// Iterator element type is `*mut A `.
41
+ /// Iterator element type is `NonNull<A> `.
38
42
#[ derive( Debug ) ]
39
43
pub struct Baseiter < A , D >
40
44
{
41
- ptr : * mut A ,
45
+ ptr : NonNull < A > ,
42
46
dim : D ,
43
47
strides : D ,
44
48
index : Option < D > ,
@@ -50,7 +54,7 @@ impl<A, D: Dimension> Baseiter<A, D>
50
54
/// to be correct to avoid performing an unsafe pointer offset while
51
55
/// iterating.
52
56
#[ inline]
53
- pub unsafe fn new ( ptr : * mut A , len : D , stride : D ) -> Baseiter < A , D >
57
+ pub unsafe fn new ( ptr : NonNull < A > , len : D , stride : D ) -> Baseiter < A , D >
54
58
{
55
59
Baseiter {
56
60
ptr,
@@ -63,10 +67,10 @@ impl<A, D: Dimension> Baseiter<A, D>
63
67
64
68
impl < A , D : Dimension > Iterator for Baseiter < A , D >
65
69
{
66
- type Item = * mut A ;
70
+ type Item = NonNull < A > ;
67
71
68
72
#[ inline]
69
- fn next ( & mut self ) -> Option < * mut A >
73
+ fn next ( & mut self ) -> Option < Self :: Item >
70
74
{
71
75
let index = match self . index {
72
76
None => return None ,
@@ -84,7 +88,7 @@ impl<A, D: Dimension> Iterator for Baseiter<A, D>
84
88
}
85
89
86
90
fn fold < Acc , G > ( mut self , init : Acc , mut g : G ) -> Acc
87
- where G : FnMut ( Acc , * mut A ) -> Acc
91
+ where G : FnMut ( Acc , Self :: Item ) -> Acc
88
92
{
89
93
let ndim = self . dim . ndim ( ) ;
90
94
debug_assert_ne ! ( ndim, 0 ) ;
@@ -133,28 +137,28 @@ impl<A, D: Dimension> ExactSizeIterator for Baseiter<A, D>
133
137
impl < A > DoubleEndedIterator for Baseiter < A , Ix1 >
134
138
{
135
139
#[ inline]
136
- fn next_back ( & mut self ) -> Option < * mut A >
140
+ fn next_back ( & mut self ) -> Option < Self :: Item >
137
141
{
138
142
let index = match self . index {
139
143
None => return None ,
140
144
Some ( ix) => ix,
141
145
} ;
142
146
self . dim [ 0 ] -= 1 ;
143
- let offset = < _ > :: stride_offset ( & self . dim , & self . strides ) ;
147
+ let offset = Ix1 :: stride_offset ( & self . dim , & self . strides ) ;
144
148
if index == self . dim {
145
149
self . index = None ;
146
150
}
147
151
148
152
unsafe { Some ( self . ptr . offset ( offset) ) }
149
153
}
150
154
151
- fn nth_back ( & mut self , n : usize ) -> Option < * mut A >
155
+ fn nth_back ( & mut self , n : usize ) -> Option < Self :: Item >
152
156
{
153
157
let index = self . index ?;
154
158
let len = self . dim [ 0 ] - index[ 0 ] ;
155
159
if n < len {
156
160
self . dim [ 0 ] -= n + 1 ;
157
- let offset = < _ > :: stride_offset ( & self . dim , & self . strides ) ;
161
+ let offset = Ix1 :: stride_offset ( & self . dim , & self . strides ) ;
158
162
if index == self . dim {
159
163
self . index = None ;
160
164
}
@@ -166,7 +170,7 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
166
170
}
167
171
168
172
fn rfold < Acc , G > ( mut self , init : Acc , mut g : G ) -> Acc
169
- where G : FnMut ( Acc , * mut A ) -> Acc
173
+ where G : FnMut ( Acc , Self :: Item ) -> Acc
170
174
{
171
175
let mut accum = init;
172
176
if let Some ( index) = self . index {
@@ -226,7 +230,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
226
230
#[ inline]
227
231
fn next ( & mut self ) -> Option < & ' a A >
228
232
{
229
- self . inner . next ( ) . map ( |p| unsafe { & * p } )
233
+ self . inner . next ( ) . map ( |p| unsafe { p . as_ref ( ) } )
230
234
}
231
235
232
236
fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -237,7 +241,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
237
241
fn fold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
238
242
where G : FnMut ( Acc , Self :: Item ) -> Acc
239
243
{
240
- 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 ( ) ) ) }
241
245
}
242
246
}
243
247
@@ -246,13 +250,13 @@ impl<'a, A> DoubleEndedIterator for ElementsBase<'a, A, Ix1>
246
250
#[ inline]
247
251
fn next_back ( & mut self ) -> Option < & ' a A >
248
252
{
249
- self . inner . next_back ( ) . map ( |p| unsafe { & * p } )
253
+ self . inner . next_back ( ) . map ( |p| unsafe { p . as_ref ( ) } )
250
254
}
251
255
252
256
fn rfold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
253
257
where G : FnMut ( Acc , Self :: Item ) -> Acc
254
258
{
255
- 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 ( ) ) ) }
256
260
}
257
261
}
258
262
@@ -646,7 +650,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
646
650
#[ inline]
647
651
fn next ( & mut self ) -> Option < & ' a mut A >
648
652
{
649
- self . inner . next ( ) . map ( |p| unsafe { & mut * p } )
653
+ self . inner . next ( ) . map ( |mut p| unsafe { p . as_mut ( ) } )
650
654
}
651
655
652
656
fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -657,7 +661,10 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
657
661
fn fold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
658
662
where G : FnMut ( Acc , Self :: Item ) -> Acc
659
663
{
660
- 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
+ }
661
668
}
662
669
}
663
670
@@ -666,13 +673,16 @@ impl<'a, A> DoubleEndedIterator for ElementsBaseMut<'a, A, Ix1>
666
673
#[ inline]
667
674
fn next_back ( & mut self ) -> Option < & ' a mut A >
668
675
{
669
- self . inner . next_back ( ) . map ( |p| unsafe { & mut * p } )
676
+ self . inner . next_back ( ) . map ( |mut p| unsafe { p . as_mut ( ) } )
670
677
}
671
678
672
679
fn rfold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
673
680
where G : FnMut ( Acc , Self :: Item ) -> Acc
674
681
{
675
- 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
+ }
676
686
}
677
687
}
678
688
@@ -748,7 +758,7 @@ where D: Dimension
748
758
{
749
759
self . iter
750
760
. next ( )
751
- . 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 ) ) } )
752
762
}
753
763
754
764
fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -772,7 +782,7 @@ impl<'a, A> DoubleEndedIterator for LanesIter<'a, A, Ix1>
772
782
{
773
783
self . iter
774
784
. next_back ( )
775
- . 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 ) ) } )
776
786
}
777
787
}
778
788
@@ -800,7 +810,7 @@ where D: Dimension
800
810
{
801
811
self . iter
802
812
. next ( )
803
- . 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 ) ) } )
804
814
}
805
815
806
816
fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -824,7 +834,7 @@ impl<'a, A> DoubleEndedIterator for LanesIterMut<'a, A, Ix1>
824
834
{
825
835
self . iter
826
836
. next_back ( )
827
- . 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 ) ) } )
828
838
}
829
839
}
830
840
0 commit comments