Skip to content

Commit f8e92cb

Browse files
committed
fix Vec<ZeroSizeType>
1 parent 1384379 commit f8e92cb

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libstd/vec.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl<T> Vec<T> {
9191
/// let vec: Vec<int> = Vec::with_capacity(10);
9292
/// ```
9393
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 } }
9495
if capacity == 0 {
9596
Vec::new()
9697
} else {
@@ -486,6 +487,7 @@ impl<T> Vec<T> {
486487
/// assert_eq!(vec.capacity(), 11);
487488
/// ```
488489
pub fn reserve_exact(&mut self, capacity: uint) {
490+
if size_of::<T>() == 0 { return }
489491
if capacity > self.cap {
490492
let size = capacity.checked_mul(&size_of::<T>()).expect("capacity overflow");
491493
unsafe {
@@ -505,6 +507,7 @@ impl<T> Vec<T> {
505507
/// vec.shrink_to_fit();
506508
/// ```
507509
pub fn shrink_to_fit(&mut self) {
510+
if size_of::<T>() == 0 { return }
508511
if self.len == 0 {
509512
if self.cap != 0 {
510513
unsafe {
@@ -559,6 +562,12 @@ impl<T> Vec<T> {
559562
/// ```
560563
#[inline]
561564
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+
}
562571
if self.len == self.cap {
563572
let old_size = self.cap * size_of::<T>();
564573
let size = max(old_size, 2 * size_of::<T>()) * 2;
@@ -1405,7 +1414,9 @@ impl<T> Drop for Vec<T> {
14051414
for x in self.as_mut_slice().iter() {
14061415
ptr::read(x);
14071416
}
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+
}
14091420
}
14101421
}
14111422
}
@@ -1460,7 +1471,9 @@ impl<T> Drop for MoveItems<T> {
14601471
if self.cap != 0 {
14611472
for _x in *self {}
14621473
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+
}
14641477
}
14651478
}
14661479
}

0 commit comments

Comments
 (0)