@@ -58,7 +58,7 @@ use core::kinds::marker::{ContravariantLifetime, InvariantType};
58
58
use core:: mem;
59
59
use core:: num:: { Int , UnsignedInt } ;
60
60
use core:: ops;
61
- use core:: ptr:: { mod, Unique } ;
61
+ use core:: ptr:: { mod, NonZero } ;
62
62
use core:: raw:: Slice as RawSlice ;
63
63
use core:: uint;
64
64
@@ -133,7 +133,7 @@ use slice::CloneSliceExt;
133
133
#[ unsafe_no_drop_flag]
134
134
#[ stable]
135
135
pub struct Vec < T > {
136
- ptr : Unique < T > ,
136
+ ptr : NonZero < * mut T > ,
137
137
len : uint ,
138
138
cap : uint ,
139
139
}
@@ -176,7 +176,7 @@ impl<T> Vec<T> {
176
176
// non-null value which is fine since we never call deallocate on the ptr
177
177
// if cap is 0. The reason for this is because the pointer of a slice
178
178
// being NULL would break the null pointer optimization for enums.
179
- Vec { ptr : Unique ( EMPTY as * mut T ) , len : 0 , cap : 0 }
179
+ Vec { ptr : NonZero ( EMPTY as * mut T ) , len : 0 , cap : 0 }
180
180
}
181
181
182
182
/// Constructs a new, empty `Vec<T>` with the specified capacity.
@@ -209,15 +209,15 @@ impl<T> Vec<T> {
209
209
#[ stable]
210
210
pub fn with_capacity ( capacity : uint ) -> Vec < T > {
211
211
if mem:: size_of :: < T > ( ) == 0 {
212
- Vec { ptr : Unique ( EMPTY as * mut T ) , len : 0 , cap : uint:: MAX }
212
+ Vec { ptr : NonZero ( EMPTY as * mut T ) , len : 0 , cap : uint:: MAX }
213
213
} else if capacity == 0 {
214
214
Vec :: new ( )
215
215
} else {
216
216
let size = capacity. checked_mul ( mem:: size_of :: < T > ( ) )
217
217
. expect ( "capacity overflow" ) ;
218
218
let ptr = unsafe { allocate ( size, mem:: min_align_of :: < T > ( ) ) } ;
219
219
if ptr. is_null ( ) { :: alloc:: oom ( ) }
220
- Vec { ptr : Unique ( ptr as * mut T ) , len : 0 , cap : capacity }
220
+ Vec { ptr : NonZero ( ptr as * mut T ) , len : 0 , cap : capacity }
221
221
}
222
222
}
223
223
@@ -284,7 +284,7 @@ impl<T> Vec<T> {
284
284
#[ unstable = "needs finalization" ]
285
285
pub unsafe fn from_raw_parts ( ptr : * mut T , length : uint ,
286
286
capacity : uint ) -> Vec < T > {
287
- Vec { ptr : Unique ( ptr) , len : length, cap : capacity }
287
+ Vec { ptr : NonZero ( ptr) , len : length, cap : capacity }
288
288
}
289
289
290
290
/// Creates a vector by copying the elements from a raw pointer.
@@ -792,22 +792,24 @@ impl<T> Vec<T> {
792
792
pub fn shrink_to_fit ( & mut self ) {
793
793
if mem:: size_of :: < T > ( ) == 0 { return }
794
794
795
+ let NonZero ( ptr) = self . ptr ;
795
796
if self . len == 0 {
796
797
if self . cap != 0 {
797
798
unsafe {
798
- dealloc ( self . ptr . 0 , self . cap )
799
+ dealloc ( ptr, self . cap )
799
800
}
800
801
self . cap = 0 ;
801
802
}
802
803
} else {
803
804
unsafe {
804
805
// Overflow check is unnecessary as the vector is already at
805
806
// least this large.
806
- self . ptr = Unique ( reallocate ( self . ptr . 0 as * mut u8 ,
807
- self . cap * mem:: size_of :: < T > ( ) ,
808
- self . len * mem:: size_of :: < T > ( ) ,
809
- mem:: min_align_of :: < T > ( ) ) as * mut T ) ;
810
- if self . ptr . 0 . is_null ( ) { :: alloc:: oom ( ) }
807
+ let ptr = reallocate ( ptr as * mut u8 ,
808
+ self . cap * mem:: size_of :: < T > ( ) ,
809
+ self . len * mem:: size_of :: < T > ( ) ,
810
+ mem:: min_align_of :: < T > ( ) ) as * mut T ;
811
+ if ptr. is_null ( ) { :: alloc:: oom ( ) }
812
+ self . ptr = NonZero ( ptr) ;
811
813
}
812
814
self . cap = self . len ;
813
815
}
@@ -865,9 +867,10 @@ impl<T> Vec<T> {
865
867
#[ inline]
866
868
#[ stable]
867
869
pub fn as_mut_slice < ' a > ( & ' a mut self ) -> & ' a mut [ T ] {
870
+ let NonZero ( ptr) = self . ptr ;
868
871
unsafe {
869
872
mem:: transmute ( RawSlice {
870
- data : self . ptr . 0 as * const T ,
873
+ data : ptr as * const T ,
871
874
len : self . len ,
872
875
} )
873
876
}
@@ -890,9 +893,9 @@ impl<T> Vec<T> {
890
893
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
891
894
pub fn into_iter ( self ) -> IntoIter < T > {
892
895
unsafe {
893
- let ptr = self . ptr . 0 ;
896
+ let NonZero ( ptr) = self . ptr ;
894
897
let cap = self . cap ;
895
- let begin = self . ptr . 0 as * const T ;
898
+ let begin = ptr as * const T ;
896
899
let end = if mem:: size_of :: < T > ( ) == 0 {
897
900
( ptr as uint + self . len ( ) ) as * const T
898
901
} else {
@@ -1110,14 +1113,16 @@ impl<T> Vec<T> {
1110
1113
let size = max ( old_size, 2 * mem:: size_of :: < T > ( ) ) * 2 ;
1111
1114
if old_size > size { panic ! ( "capacity overflow" ) }
1112
1115
unsafe {
1113
- self . ptr = Unique ( alloc_or_realloc ( self . ptr . 0 , old_size, size) ) ;
1114
- if self . ptr . 0 . is_null ( ) { :: alloc:: oom ( ) }
1116
+ let NonZero ( ptr) = self . ptr ;
1117
+ let ptr = alloc_or_realloc ( ptr, old_size, size) ;
1118
+ if ptr. is_null ( ) { :: alloc:: oom ( ) }
1119
+ self . ptr = NonZero ( ptr) ;
1115
1120
}
1116
1121
self . cap = max ( self . cap , 2 ) * 2 ;
1117
1122
}
1118
1123
1119
1124
unsafe {
1120
- let end = self . ptr . 0 . offset ( self . len as int ) ;
1125
+ let NonZero ( end) = self . ptr . offset ( self . len as int ) ;
1121
1126
ptr:: write ( & mut * end, value) ;
1122
1127
self . len += 1 ;
1123
1128
}
@@ -1231,10 +1236,10 @@ impl<T> Vec<T> {
1231
1236
let size = capacity. checked_mul ( mem:: size_of :: < T > ( ) )
1232
1237
. expect ( "capacity overflow" ) ;
1233
1238
unsafe {
1234
- self . ptr = Unique ( alloc_or_realloc ( self . ptr . 0 ,
1235
- self . cap * mem:: size_of :: < T > ( ) ,
1236
- size ) ) ;
1237
- if self . ptr . 0 . is_null ( ) { :: alloc :: oom ( ) }
1239
+ let NonZero ( ptr) = self . ptr ;
1240
+ let ptr = alloc_or_realloc ( ptr , self . cap * mem:: size_of :: < T > ( ) , size ) ;
1241
+ if ptr . is_null ( ) { :: alloc :: oom ( ) }
1242
+ self . ptr = NonZero ( ptr ) ;
1238
1243
}
1239
1244
self . cap = capacity;
1240
1245
}
@@ -1355,9 +1360,10 @@ impl<T> AsSlice<T> for Vec<T> {
1355
1360
#[ inline]
1356
1361
#[ stable]
1357
1362
fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
1363
+ let NonZero ( ptr) = self . ptr ;
1358
1364
unsafe {
1359
1365
mem:: transmute ( RawSlice {
1360
- data : self . ptr . 0 as * const T ,
1366
+ data : ptr as * const T ,
1361
1367
len : self . len
1362
1368
} )
1363
1369
}
@@ -1382,7 +1388,8 @@ impl<T> Drop for Vec<T> {
1382
1388
for x in self . iter ( ) {
1383
1389
ptr:: read ( x) ;
1384
1390
}
1385
- dealloc ( self . ptr . 0 , self . cap )
1391
+ let NonZero ( ptr) = self . ptr ;
1392
+ dealloc ( ptr, self . cap )
1386
1393
}
1387
1394
}
1388
1395
}
@@ -1420,7 +1427,7 @@ impl<T> IntoIter<T> {
1420
1427
for _x in self { }
1421
1428
let IntoIter { allocation, cap, ptr : _ptr, end : _end } = self ;
1422
1429
mem:: forget ( self ) ;
1423
- Vec { ptr : Unique ( allocation) , cap : cap, len : 0 }
1430
+ Vec { ptr : NonZero ( allocation) , cap : cap, len : 0 }
1424
1431
}
1425
1432
}
1426
1433
0 commit comments