@@ -91,6 +91,7 @@ impl<T> Vec<T> {
91
91
/// let vec: Vec<int> = Vec::with_capacity(10);
92
92
/// ```
93
93
pub fn with_capacity ( capacity : uint ) -> Vec < T > {
94
+ if size_of :: < T > ( ) == 0 { return Vec { len : 0 , cap : :: uint:: MAX , ptr : 0 as * mut T } }
94
95
if capacity == 0 {
95
96
Vec :: new ( )
96
97
} else {
@@ -486,6 +487,7 @@ impl<T> Vec<T> {
486
487
/// assert_eq!(vec.capacity(), 11);
487
488
/// ```
488
489
pub fn reserve_exact ( & mut self , capacity : uint ) {
490
+ if size_of :: < T > ( ) == 0 { return }
489
491
if capacity > self . cap {
490
492
let size = capacity. checked_mul ( & size_of :: < T > ( ) ) . expect ( "capacity overflow" ) ;
491
493
unsafe {
@@ -505,6 +507,7 @@ impl<T> Vec<T> {
505
507
/// vec.shrink_to_fit();
506
508
/// ```
507
509
pub fn shrink_to_fit ( & mut self ) {
510
+ if size_of :: < T > ( ) == 0 { return }
508
511
if self . len == 0 {
509
512
if self . cap != 0 {
510
513
unsafe {
@@ -559,6 +562,12 @@ impl<T> Vec<T> {
559
562
/// ```
560
563
#[ inline]
561
564
pub fn push ( & mut self , value : T ) {
565
+ if size_of :: < T > ( ) == 0 {
566
+ // zero-size types consume no memory, so we can't rely on the address space running out
567
+ self . len = self . len . checked_add ( & 1 ) . expect ( "length overflow" ) ;
568
+ unsafe { forget ( value) ; }
569
+ return
570
+ }
562
571
if self . len == self . cap {
563
572
let old_size = self . cap * size_of :: < T > ( ) ;
564
573
let size = max ( old_size, 2 * size_of :: < T > ( ) ) * 2 ;
@@ -1405,7 +1414,9 @@ impl<T> Drop for Vec<T> {
1405
1414
for x in self . as_mut_slice ( ) . iter ( ) {
1406
1415
ptr:: read ( x) ;
1407
1416
}
1408
- deallocate ( self . ptr as * mut u8 , self . cap * size_of :: < T > ( ) , min_align_of :: < T > ( ) )
1417
+ if size_of :: < T > ( ) != 0 {
1418
+ deallocate ( self . ptr as * mut u8 , self . cap * size_of :: < T > ( ) , min_align_of :: < T > ( ) )
1419
+ }
1409
1420
}
1410
1421
}
1411
1422
}
@@ -1460,7 +1471,9 @@ impl<T> Drop for MoveItems<T> {
1460
1471
if self . cap != 0 {
1461
1472
for _x in * self { }
1462
1473
unsafe {
1463
- deallocate ( self . allocation , self . cap * size_of :: < T > ( ) , min_align_of :: < T > ( ) )
1474
+ if size_of :: < T > ( ) != 0 {
1475
+ deallocate ( self . allocation , self . cap * size_of :: < T > ( ) , min_align_of :: < T > ( ) )
1476
+ }
1464
1477
}
1465
1478
}
1466
1479
}
0 commit comments