@@ -19,6 +19,10 @@ use alloc::vec::Vec;
1919use std:: iter:: FromIterator ;
2020use std:: marker:: PhantomData ;
2121use std:: ptr;
22+ use std:: ptr:: NonNull ;
23+
24+ #[ allow( unused_imports) ] // Needed for Rust 1.64
25+ use rawpointer:: PointerExt ;
2226
2327use crate :: Ix1 ;
2428
@@ -34,11 +38,11 @@ use std::slice::{self, Iter as SliceIter, IterMut as SliceIterMut};
3438
3539/// Base for iterators over all axes.
3640///
37- /// Iterator element type is `*mut A `.
41+ /// Iterator element type is `NonNull<A> `.
3842#[ derive( Debug ) ]
3943pub struct Baseiter < A , D >
4044{
41- ptr : * mut A ,
45+ ptr : NonNull < A > ,
4246 dim : D ,
4347 strides : D ,
4448 index : Option < D > ,
@@ -50,7 +54,7 @@ impl<A, D: Dimension> Baseiter<A, D>
5054 /// to be correct to avoid performing an unsafe pointer offset while
5155 /// iterating.
5256 #[ 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 >
5458 {
5559 Baseiter {
5660 ptr,
@@ -63,10 +67,10 @@ impl<A, D: Dimension> Baseiter<A, D>
6367
6468impl < A , D : Dimension > Iterator for Baseiter < A , D >
6569{
66- type Item = * mut A ;
70+ type Item = NonNull < A > ;
6771
6872 #[ inline]
69- fn next ( & mut self ) -> Option < * mut A >
73+ fn next ( & mut self ) -> Option < Self :: Item >
7074 {
7175 let index = match self . index {
7276 None => return None ,
@@ -84,7 +88,7 @@ impl<A, D: Dimension> Iterator for Baseiter<A, D>
8488 }
8589
8690 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
8892 {
8993 let ndim = self . dim . ndim ( ) ;
9094 debug_assert_ne ! ( ndim, 0 ) ;
@@ -133,28 +137,28 @@ impl<A, D: Dimension> ExactSizeIterator for Baseiter<A, D>
133137impl < A > DoubleEndedIterator for Baseiter < A , Ix1 >
134138{
135139 #[ inline]
136- fn next_back ( & mut self ) -> Option < * mut A >
140+ fn next_back ( & mut self ) -> Option < Self :: Item >
137141 {
138142 let index = match self . index {
139143 None => return None ,
140144 Some ( ix) => ix,
141145 } ;
142146 self . dim [ 0 ] -= 1 ;
143- let offset = < _ > :: stride_offset ( & self . dim , & self . strides ) ;
147+ let offset = Ix1 :: stride_offset ( & self . dim , & self . strides ) ;
144148 if index == self . dim {
145149 self . index = None ;
146150 }
147151
148152 unsafe { Some ( self . ptr . offset ( offset) ) }
149153 }
150154
151- fn nth_back ( & mut self , n : usize ) -> Option < * mut A >
155+ fn nth_back ( & mut self , n : usize ) -> Option < Self :: Item >
152156 {
153157 let index = self . index ?;
154158 let len = self . dim [ 0 ] - index[ 0 ] ;
155159 if n < len {
156160 self . dim [ 0 ] -= n + 1 ;
157- let offset = < _ > :: stride_offset ( & self . dim , & self . strides ) ;
161+ let offset = Ix1 :: stride_offset ( & self . dim , & self . strides ) ;
158162 if index == self . dim {
159163 self . index = None ;
160164 }
@@ -166,7 +170,7 @@ impl<A> DoubleEndedIterator for Baseiter<A, Ix1>
166170 }
167171
168172 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
170174 {
171175 let mut accum = init;
172176 if let Some ( index) = self . index {
@@ -226,7 +230,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
226230 #[ inline]
227231 fn next ( & mut self ) -> Option < & ' a A >
228232 {
229- self . inner . next ( ) . map ( |p| unsafe { & * p } )
233+ self . inner . next ( ) . map ( |p| unsafe { p . as_ref ( ) } )
230234 }
231235
232236 fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -237,7 +241,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBase<'a, A, D>
237241 fn fold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
238242 where G : FnMut ( Acc , Self :: Item ) -> Acc
239243 {
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 ( ) ) ) }
241245 }
242246}
243247
@@ -246,13 +250,13 @@ impl<'a, A> DoubleEndedIterator for ElementsBase<'a, A, Ix1>
246250 #[ inline]
247251 fn next_back ( & mut self ) -> Option < & ' a A >
248252 {
249- self . inner . next_back ( ) . map ( |p| unsafe { & * p } )
253+ self . inner . next_back ( ) . map ( |p| unsafe { p . as_ref ( ) } )
250254 }
251255
252256 fn rfold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
253257 where G : FnMut ( Acc , Self :: Item ) -> Acc
254258 {
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 ( ) ) ) }
256260 }
257261}
258262
@@ -646,7 +650,7 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
646650 #[ inline]
647651 fn next ( & mut self ) -> Option < & ' a mut A >
648652 {
649- self . inner . next ( ) . map ( |p| unsafe { & mut * p } )
653+ self . inner . next ( ) . map ( |mut p| unsafe { p . as_mut ( ) } )
650654 }
651655
652656 fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -657,7 +661,10 @@ impl<'a, A, D: Dimension> Iterator for ElementsBaseMut<'a, A, D>
657661 fn fold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
658662 where G : FnMut ( Acc , Self :: Item ) -> Acc
659663 {
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+ }
661668 }
662669}
663670
@@ -666,13 +673,16 @@ impl<'a, A> DoubleEndedIterator for ElementsBaseMut<'a, A, Ix1>
666673 #[ inline]
667674 fn next_back ( & mut self ) -> Option < & ' a mut A >
668675 {
669- self . inner . next_back ( ) . map ( |p| unsafe { & mut * p } )
676+ self . inner . next_back ( ) . map ( |mut p| unsafe { p . as_mut ( ) } )
670677 }
671678
672679 fn rfold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
673680 where G : FnMut ( Acc , Self :: Item ) -> Acc
674681 {
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+ }
676686 }
677687}
678688
@@ -748,7 +758,7 @@ where D: Dimension
748758 {
749759 self . iter
750760 . 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 ) ) } )
752762 }
753763
754764 fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -772,7 +782,7 @@ impl<'a, A> DoubleEndedIterator for LanesIter<'a, A, Ix1>
772782 {
773783 self . iter
774784 . 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 ) ) } )
776786 }
777787}
778788
@@ -800,7 +810,7 @@ where D: Dimension
800810 {
801811 self . iter
802812 . 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 ) ) } )
804814 }
805815
806816 fn size_hint ( & self ) -> ( usize , Option < usize > )
@@ -824,7 +834,7 @@ impl<'a, A> DoubleEndedIterator for LanesIterMut<'a, A, Ix1>
824834 {
825835 self . iter
826836 . 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 ) ) } )
828838 }
829839}
830840
0 commit comments