Skip to content

Commit bb44737

Browse files
committed
libcollections: Use NonZero in Vec.
1 parent ef5da14 commit bb44737

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/libcollections/vec.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use core::kinds::marker::{ContravariantLifetime, InvariantType};
5858
use core::mem;
5959
use core::num::{Int, UnsignedInt};
6060
use core::ops;
61-
use core::ptr::{mod, Unique};
61+
use core::ptr::{mod, NonZero};
6262
use core::raw::Slice as RawSlice;
6363
use core::uint;
6464

@@ -133,7 +133,7 @@ use slice::CloneSliceExt;
133133
#[unsafe_no_drop_flag]
134134
#[stable]
135135
pub struct Vec<T> {
136-
ptr: Unique<T>,
136+
ptr: NonZero<*mut T>,
137137
len: uint,
138138
cap: uint,
139139
}
@@ -176,7 +176,7 @@ impl<T> Vec<T> {
176176
// non-null value which is fine since we never call deallocate on the ptr
177177
// if cap is 0. The reason for this is because the pointer of a slice
178178
// 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 }
180180
}
181181

182182
/// Constructs a new, empty `Vec<T>` with the specified capacity.
@@ -209,15 +209,15 @@ impl<T> Vec<T> {
209209
#[stable]
210210
pub fn with_capacity(capacity: uint) -> Vec<T> {
211211
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 }
213213
} else if capacity == 0 {
214214
Vec::new()
215215
} else {
216216
let size = capacity.checked_mul(mem::size_of::<T>())
217217
.expect("capacity overflow");
218218
let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
219219
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 }
221221
}
222222
}
223223

@@ -284,7 +284,7 @@ impl<T> Vec<T> {
284284
#[unstable = "needs finalization"]
285285
pub unsafe fn from_raw_parts(ptr: *mut T, length: uint,
286286
capacity: uint) -> Vec<T> {
287-
Vec { ptr: Unique(ptr), len: length, cap: capacity }
287+
Vec { ptr: NonZero(ptr), len: length, cap: capacity }
288288
}
289289

290290
/// Creates a vector by copying the elements from a raw pointer.
@@ -792,22 +792,24 @@ impl<T> Vec<T> {
792792
pub fn shrink_to_fit(&mut self) {
793793
if mem::size_of::<T>() == 0 { return }
794794

795+
let NonZero(ptr) = self.ptr;
795796
if self.len == 0 {
796797
if self.cap != 0 {
797798
unsafe {
798-
dealloc(self.ptr.0, self.cap)
799+
dealloc(ptr, self.cap)
799800
}
800801
self.cap = 0;
801802
}
802803
} else {
803804
unsafe {
804805
// Overflow check is unnecessary as the vector is already at
805806
// 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);
811813
}
812814
self.cap = self.len;
813815
}
@@ -865,9 +867,10 @@ impl<T> Vec<T> {
865867
#[inline]
866868
#[stable]
867869
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
870+
let NonZero(ptr) = self.ptr;
868871
unsafe {
869872
mem::transmute(RawSlice {
870-
data: self.ptr.0 as *const T,
873+
data: ptr as *const T,
871874
len: self.len,
872875
})
873876
}
@@ -890,9 +893,9 @@ impl<T> Vec<T> {
890893
#[unstable = "matches collection reform specification, waiting for dust to settle"]
891894
pub fn into_iter(self) -> IntoIter<T> {
892895
unsafe {
893-
let ptr = self.ptr.0;
896+
let NonZero(ptr) = self.ptr;
894897
let cap = self.cap;
895-
let begin = self.ptr.0 as *const T;
898+
let begin = ptr as *const T;
896899
let end = if mem::size_of::<T>() == 0 {
897900
(ptr as uint + self.len()) as *const T
898901
} else {
@@ -1110,14 +1113,16 @@ impl<T> Vec<T> {
11101113
let size = max(old_size, 2 * mem::size_of::<T>()) * 2;
11111114
if old_size > size { panic!("capacity overflow") }
11121115
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);
11151120
}
11161121
self.cap = max(self.cap, 2) * 2;
11171122
}
11181123

11191124
unsafe {
1120-
let end = self.ptr.0.offset(self.len as int);
1125+
let NonZero(end) = self.ptr.offset(self.len as int);
11211126
ptr::write(&mut *end, value);
11221127
self.len += 1;
11231128
}
@@ -1231,10 +1236,10 @@ impl<T> Vec<T> {
12311236
let size = capacity.checked_mul(mem::size_of::<T>())
12321237
.expect("capacity overflow");
12331238
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);
12381243
}
12391244
self.cap = capacity;
12401245
}
@@ -1355,9 +1360,10 @@ impl<T> AsSlice<T> for Vec<T> {
13551360
#[inline]
13561361
#[stable]
13571362
fn as_slice<'a>(&'a self) -> &'a [T] {
1363+
let NonZero(ptr) = self.ptr;
13581364
unsafe {
13591365
mem::transmute(RawSlice {
1360-
data: self.ptr.0 as *const T,
1366+
data: ptr as *const T,
13611367
len: self.len
13621368
})
13631369
}
@@ -1382,7 +1388,8 @@ impl<T> Drop for Vec<T> {
13821388
for x in self.iter() {
13831389
ptr::read(x);
13841390
}
1385-
dealloc(self.ptr.0, self.cap)
1391+
let NonZero(ptr) = self.ptr;
1392+
dealloc(ptr, self.cap)
13861393
}
13871394
}
13881395
}
@@ -1420,7 +1427,7 @@ impl<T> IntoIter<T> {
14201427
for _x in self { }
14211428
let IntoIter { allocation, cap, ptr: _ptr, end: _end } = self;
14221429
mem::forget(self);
1423-
Vec { ptr: Unique(allocation), cap: cap, len: 0 }
1430+
Vec { ptr: NonZero(allocation), cap: cap, len: 0 }
14241431
}
14251432
}
14261433

0 commit comments

Comments
 (0)