@@ -34,7 +34,7 @@ use crate::mem;
34
34
use crate :: ops:: { self , FnMut , Range } ;
35
35
use crate :: option:: Option ;
36
36
use crate :: option:: Option :: { None , Some } ;
37
- use crate :: ptr;
37
+ use crate :: ptr:: { NonNull , self } ;
38
38
use crate :: result:: Result ;
39
39
use crate :: result:: Result :: { Err , Ok } ;
40
40
@@ -628,7 +628,7 @@ impl<T> [T] {
628
628
ptr. add ( self . len ( ) )
629
629
} ;
630
630
631
- Iter { ptr, end, _marker : marker:: PhantomData }
631
+ Iter { ptr : NonNull :: new_unchecked ( ptr as * mut T ) , end, _marker : marker:: PhantomData }
632
632
}
633
633
}
634
634
@@ -656,7 +656,7 @@ impl<T> [T] {
656
656
ptr. add ( self . len ( ) )
657
657
} ;
658
658
659
- IterMut { ptr, end, _marker : marker:: PhantomData }
659
+ IterMut { ptr : NonNull :: new_unchecked ( ptr ) , end, _marker : marker:: PhantomData }
660
660
}
661
661
}
662
662
@@ -3095,7 +3095,7 @@ macro_rules! is_empty {
3095
3095
// The way we encode the length of a ZST iterator, this works both for ZST
3096
3096
// and non-ZST.
3097
3097
( $self: ident) => {
3098
- $self. ptr == $self. end
3098
+ $self. ptr. as_ptr ( ) as * const T == $self. end
3099
3099
} ;
3100
3100
}
3101
3101
// To get rid of some bounds checks (see `position`), we compute the length in a somewhat
@@ -3105,17 +3105,17 @@ macro_rules! len {
3105
3105
#![ allow( unused_unsafe) ] // we're sometimes used within an unsafe block
3106
3106
3107
3107
let start = $self. ptr;
3108
- let size = size_from_ptr( start) ;
3108
+ let size = size_from_ptr( start. as_ptr ( ) ) ;
3109
3109
if size == 0 {
3110
3110
// This _cannot_ use `unchecked_sub` because we depend on wrapping
3111
3111
// to represent the length of long ZST slice iterators.
3112
- ( $self. end as usize ) . wrapping_sub( start as usize )
3112
+ ( $self. end as usize ) . wrapping_sub( start. as_ptr ( ) as usize )
3113
3113
} else {
3114
3114
// We know that `start <= end`, so can do better than `offset_from`,
3115
3115
// which needs to deal in signed. By setting appropriate flags here
3116
3116
// we can tell LLVM this, which helps it remove bounds checks.
3117
3117
// SAFETY: By the type invariant, `start <= end`
3118
- let diff = unsafe { unchecked_sub( $self. end as usize , start as usize ) } ;
3118
+ let diff = unsafe { unchecked_sub( $self. end as usize , start. as_ptr ( ) as usize ) } ;
3119
3119
// By also telling LLVM that the pointers are apart by an exact
3120
3120
// multiple of the type size, it can optimize `len() == 0` down to
3121
3121
// `start == end` instead of `(end - start) < size`.
@@ -3161,7 +3161,7 @@ macro_rules! iterator {
3161
3161
// Helper function for creating a slice from the iterator.
3162
3162
#[ inline( always) ]
3163
3163
fn make_slice( & self ) -> & ' a [ T ] {
3164
- unsafe { from_raw_parts( self . ptr, len!( self ) ) }
3164
+ unsafe { from_raw_parts( self . ptr. as_ptr ( ) , len!( self ) ) }
3165
3165
}
3166
3166
3167
3167
// Helper function for moving the start of the iterator forwards by `offset` elements,
@@ -3171,10 +3171,10 @@ macro_rules! iterator {
3171
3171
unsafe fn post_inc_start( & mut self , offset: isize ) -> * $raw_mut T {
3172
3172
if mem:: size_of:: <T >( ) == 0 {
3173
3173
zst_shrink!( self , offset) ;
3174
- self . ptr
3174
+ self . ptr. as_ptr ( )
3175
3175
} else {
3176
- let old = self . ptr;
3177
- self . ptr = self . ptr. offset( offset) ;
3176
+ let old = self . ptr. as_ptr ( ) ;
3177
+ self . ptr = NonNull :: new_unchecked ( self . ptr. as_ptr ( ) . offset( offset) ) ;
3178
3178
old
3179
3179
}
3180
3180
}
@@ -3186,7 +3186,7 @@ macro_rules! iterator {
3186
3186
unsafe fn pre_dec_end( & mut self , offset: isize ) -> * $raw_mut T {
3187
3187
if mem:: size_of:: <T >( ) == 0 {
3188
3188
zst_shrink!( self , offset) ;
3189
- self . ptr
3189
+ self . ptr. as_ptr ( )
3190
3190
} else {
3191
3191
self . end = self . end. offset( -offset) ;
3192
3192
self . end
@@ -3215,7 +3215,7 @@ macro_rules! iterator {
3215
3215
fn next( & mut self ) -> Option <$elem> {
3216
3216
// could be implemented with slices, but this avoids bounds checks
3217
3217
unsafe {
3218
- assume( !self . ptr. is_null( ) ) ;
3218
+ assume( !self . ptr. as_ptr ( ) . is_null( ) ) ;
3219
3219
if mem:: size_of:: <T >( ) != 0 {
3220
3220
assume( !self . end. is_null( ) ) ;
3221
3221
}
@@ -3245,9 +3245,12 @@ macro_rules! iterator {
3245
3245
if mem:: size_of:: <T >( ) == 0 {
3246
3246
// We have to do it this way as `ptr` may never be 0, but `end`
3247
3247
// could be (due to wrapping).
3248
- self . end = self . ptr;
3248
+ self . end = self . ptr. as_ptr ( ) ;
3249
3249
} else {
3250
- self . ptr = self . end;
3250
+ unsafe {
3251
+ // End can't be 0 if T isn't ZST because ptr isn't 0 and end >= ptr
3252
+ self . ptr = NonNull :: new_unchecked( self . end as * mut T ) ;
3253
+ }
3251
3254
}
3252
3255
return None ;
3253
3256
}
@@ -3308,7 +3311,7 @@ macro_rules! iterator {
3308
3311
fn next_back( & mut self ) -> Option <$elem> {
3309
3312
// could be implemented with slices, but this avoids bounds checks
3310
3313
unsafe {
3311
- assume( !self . ptr. is_null( ) ) ;
3314
+ assume( !self . ptr. as_ptr ( ) . is_null( ) ) ;
3312
3315
if mem:: size_of:: <T >( ) != 0 {
3313
3316
assume( !self . end. is_null( ) ) ;
3314
3317
}
@@ -3324,7 +3327,7 @@ macro_rules! iterator {
3324
3327
fn nth_back( & mut self , n: usize ) -> Option <$elem> {
3325
3328
if n >= len!( self ) {
3326
3329
// This iterator is now empty.
3327
- self . end = self . ptr;
3330
+ self . end = self . ptr. as_ptr ( ) ;
3328
3331
return None ;
3329
3332
}
3330
3333
// We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
@@ -3365,7 +3368,7 @@ macro_rules! iterator {
3365
3368
/// [slices]: ../../std/primitive.slice.html
3366
3369
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3367
3370
pub struct Iter < ' a , T : ' a > {
3368
- ptr : * const T ,
3371
+ ptr : NonNull < T > ,
3369
3372
end : * const T , // If T is a ZST, this is actually ptr+len. This encoding is picked so that
3370
3373
// ptr == end is a quick test for the Iterator being empty, that works
3371
3374
// for both ZST and non-ZST.
@@ -3467,7 +3470,7 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
3467
3470
/// [slices]: ../../std/primitive.slice.html
3468
3471
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3469
3472
pub struct IterMut < ' a , T : ' a > {
3470
- ptr : * mut T ,
3473
+ ptr : NonNull < T > ,
3471
3474
end : * mut T , // If T is a ZST, this is actually ptr+len. This encoding is picked so that
3472
3475
// ptr == end is a quick test for the Iterator being empty, that works
3473
3476
// for both ZST and non-ZST.
@@ -3522,7 +3525,7 @@ impl<'a, T> IterMut<'a, T> {
3522
3525
/// ```
3523
3526
#[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
3524
3527
pub fn into_slice ( self ) -> & ' a mut [ T ] {
3525
- unsafe { from_raw_parts_mut ( self . ptr , len ! ( self ) ) }
3528
+ unsafe { from_raw_parts_mut ( self . ptr . as_ptr ( ) , len ! ( self ) ) }
3526
3529
}
3527
3530
3528
3531
/// Views the underlying data as a subslice of the original data.
@@ -5682,7 +5685,7 @@ impl_marker_for!(BytewiseEquality,
5682
5685
#[ doc( hidden) ]
5683
5686
unsafe impl < ' a , T > TrustedRandomAccess for Iter < ' a , T > {
5684
5687
unsafe fn get_unchecked ( & mut self , i : usize ) -> & ' a T {
5685
- & * self . ptr . add ( i)
5688
+ & * self . ptr . as_ptr ( ) . add ( i)
5686
5689
}
5687
5690
fn may_have_side_effect ( ) -> bool {
5688
5691
false
@@ -5692,7 +5695,7 @@ unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> {
5692
5695
#[ doc( hidden) ]
5693
5696
unsafe impl < ' a , T > TrustedRandomAccess for IterMut < ' a , T > {
5694
5697
unsafe fn get_unchecked ( & mut self , i : usize ) -> & ' a mut T {
5695
- & mut * self . ptr . add ( i)
5698
+ & mut * self . ptr . as_ptr ( ) . add ( i)
5696
5699
}
5697
5700
fn may_have_side_effect ( ) -> bool {
5698
5701
false
0 commit comments