@@ -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 : NonZero ( EMPTY as * mut T ) , len : 0 , cap : 0 }
179
+ Vec { ptr : unsafe { NonZero :: new ( 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 : NonZero ( EMPTY as * mut T ) , len : 0 , cap : uint:: MAX }
212
+ Vec { ptr : unsafe { NonZero :: new ( 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 : NonZero ( ptr as * mut T ) , len : 0 , cap : capacity }
220
+ Vec { ptr : unsafe { NonZero :: new ( 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 : NonZero ( ptr) , len : length, cap : capacity }
287
+ Vec { ptr : NonZero :: new ( ptr) , len : length, cap : capacity }
288
288
}
289
289
290
290
/// Creates a vector by copying the elements from a raw pointer.
@@ -792,24 +792,23 @@ 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 ;
796
795
if self . len == 0 {
797
796
if self . cap != 0 {
798
797
unsafe {
799
- dealloc ( ptr, self . cap )
798
+ dealloc ( * self . ptr , self . cap )
800
799
}
801
800
self . cap = 0 ;
802
801
}
803
802
} else {
804
803
unsafe {
805
804
// Overflow check is unnecessary as the vector is already at
806
805
// least this large.
807
- let ptr = reallocate ( ptr as * mut u8 ,
806
+ let ptr = reallocate ( * self . ptr as * mut u8 ,
808
807
self . cap * mem:: size_of :: < T > ( ) ,
809
808
self . len * mem:: size_of :: < T > ( ) ,
810
809
mem:: min_align_of :: < T > ( ) ) as * mut T ;
811
810
if ptr. is_null ( ) { :: alloc:: oom ( ) }
812
- self . ptr = NonZero ( ptr) ;
811
+ self . ptr = NonZero :: new ( ptr) ;
813
812
}
814
813
self . cap = self . len ;
815
814
}
@@ -867,10 +866,9 @@ impl<T> Vec<T> {
867
866
#[ inline]
868
867
#[ stable]
869
868
pub fn as_mut_slice < ' a > ( & ' a mut self ) -> & ' a mut [ T ] {
870
- let NonZero ( ptr) = self . ptr ;
871
869
unsafe {
872
870
mem:: transmute ( RawSlice {
873
- data : ptr as * const T ,
871
+ data : * self . ptr as * const T ,
874
872
len : self . len ,
875
873
} )
876
874
}
@@ -893,7 +891,7 @@ impl<T> Vec<T> {
893
891
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
894
892
pub fn into_iter ( self ) -> IntoIter < T > {
895
893
unsafe {
896
- let NonZero ( ptr) = self . ptr ;
894
+ let ptr = * self . ptr ;
897
895
let cap = self . cap ;
898
896
let begin = ptr as * const T ;
899
897
let end = if mem:: size_of :: < T > ( ) == 0 {
@@ -1113,16 +1111,15 @@ impl<T> Vec<T> {
1113
1111
let size = max ( old_size, 2 * mem:: size_of :: < T > ( ) ) * 2 ;
1114
1112
if old_size > size { panic ! ( "capacity overflow" ) }
1115
1113
unsafe {
1116
- let NonZero ( ptr) = self . ptr ;
1117
- let ptr = alloc_or_realloc ( ptr, old_size, size) ;
1114
+ let ptr = alloc_or_realloc ( * self . ptr , old_size, size) ;
1118
1115
if ptr. is_null ( ) { :: alloc:: oom ( ) }
1119
- self . ptr = NonZero ( ptr) ;
1116
+ self . ptr = NonZero :: new ( ptr) ;
1120
1117
}
1121
1118
self . cap = max ( self . cap , 2 ) * 2 ;
1122
1119
}
1123
1120
1124
1121
unsafe {
1125
- let NonZero ( end) = self . ptr . offset ( self . len as int ) ;
1122
+ let end = * self . ptr . offset ( self . len as int ) ;
1126
1123
ptr:: write ( & mut * end, value) ;
1127
1124
self . len += 1 ;
1128
1125
}
@@ -1167,11 +1164,11 @@ impl<T> Vec<T> {
1167
1164
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
1168
1165
pub fn drain < ' a > ( & ' a mut self ) -> Drain < ' a , T > {
1169
1166
unsafe {
1170
- let begin = self . ptr . 0 as * const T ;
1167
+ let begin = * self . ptr as * const T ;
1171
1168
let end = if mem:: size_of :: < T > ( ) == 0 {
1172
- ( self . ptr . 0 as uint + self . len ( ) ) as * const T
1169
+ ( * self . ptr as uint + self . len ( ) ) as * const T
1173
1170
} else {
1174
- self . ptr . 0 . offset ( self . len ( ) as int ) as * const T
1171
+ ( * self . ptr ) . offset ( self . len ( ) as int ) as * const T
1175
1172
} ;
1176
1173
self . set_len ( 0 ) ;
1177
1174
Drain {
@@ -1236,10 +1233,9 @@ impl<T> Vec<T> {
1236
1233
let size = capacity. checked_mul ( mem:: size_of :: < T > ( ) )
1237
1234
. expect ( "capacity overflow" ) ;
1238
1235
unsafe {
1239
- let NonZero ( ptr) = self . ptr ;
1240
- let ptr = alloc_or_realloc ( ptr, self . cap * mem:: size_of :: < T > ( ) , size) ;
1236
+ let ptr = alloc_or_realloc ( * self . ptr , self . cap * mem:: size_of :: < T > ( ) , size) ;
1241
1237
if ptr. is_null ( ) { :: alloc:: oom ( ) }
1242
- self . ptr = NonZero ( ptr) ;
1238
+ self . ptr = NonZero :: new ( ptr) ;
1243
1239
}
1244
1240
self . cap = capacity;
1245
1241
}
@@ -1360,10 +1356,9 @@ impl<T> AsSlice<T> for Vec<T> {
1360
1356
#[ inline]
1361
1357
#[ stable]
1362
1358
fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
1363
- let NonZero ( ptr) = self . ptr ;
1364
1359
unsafe {
1365
1360
mem:: transmute ( RawSlice {
1366
- data : ptr as * const T ,
1361
+ data : * self . ptr as * const T ,
1367
1362
len : self . len
1368
1363
} )
1369
1364
}
@@ -1388,8 +1383,7 @@ impl<T> Drop for Vec<T> {
1388
1383
for x in self . iter ( ) {
1389
1384
ptr:: read ( x) ;
1390
1385
}
1391
- let NonZero ( ptr) = self . ptr ;
1392
- dealloc ( ptr, self . cap )
1386
+ dealloc ( * self . ptr , self . cap )
1393
1387
}
1394
1388
}
1395
1389
}
@@ -1427,7 +1421,7 @@ impl<T> IntoIter<T> {
1427
1421
for _x in self { }
1428
1422
let IntoIter { allocation, cap, ptr : _ptr, end : _end } = self ;
1429
1423
mem:: forget ( self ) ;
1430
- Vec { ptr : NonZero ( allocation) , cap : cap, len : 0 }
1424
+ Vec { ptr : NonZero :: new ( allocation) , cap : cap, len : 0 }
1431
1425
}
1432
1426
}
1433
1427
0 commit comments