From f94d671bfae5d8e9a4a4add310b1c40af0ab62a6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 9 May 2014 10:34:51 -0700 Subject: core: Remove the cast module This commit revisits the `cast` module in libcore and libstd, and scrutinizes all functions inside of it. The result was to remove the `cast` module entirely, folding all functionality into the `mem` module. Specifically, this is the fate of each function in the `cast` module. * transmute - This function was moved to `mem`, but it is now marked as #[unstable]. This is due to planned changes to the `transmute` function and how it can be invoked (see the #[unstable] comment). For more information, see RFC 5 and #12898 * transmute_copy - This function was moved to `mem`, with clarification that is is not an error to invoke it with T/U that are different sizes, but rather that it is strongly discouraged. This function is now #[stable] * forget - This function was moved to `mem` and marked #[stable] * bump_box_refcount - This function was removed due to the deprecation of managed boxes as well as its questionable utility. * transmute_mut - This function was previously deprecated, and removed as part of this commit. * transmute_mut_unsafe - This function doesn't serve much of a purpose when it can be achieved with an `as` in safe code, so it was removed. * transmute_lifetime - This function was removed because it is likely a strong indication that code is incorrect in the first place. * transmute_mut_lifetime - This function was removed for the same reasons as `transmute_lifetime` * copy_lifetime - This function was moved to `mem`, but it is marked `#[unstable]` now due to the likelihood of being removed in the future if it is found to not be very useful. * copy_mut_lifetime - This function was also moved to `mem`, but had the same treatment as `copy_lifetime`. * copy_lifetime_vec - This function was removed because it is not used today, and its existence is not necessary with DST (copy_lifetime will suffice). In summary, the cast module was stripped down to these functions, and then the functions were moved to the `mem` module. transmute - #[unstable] transmute_copy - #[stable] forget - #[stable] copy_lifetime - #[unstable] copy_mut_lifetime - #[unstable] [breaking-change] --- src/libstd/vec.rs | 82 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 34 deletions(-) (limited to 'src/libstd/vec.rs') diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index aa10be1d1be..528ab72762a 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -10,23 +10,21 @@ //! An owned, growable vector. -use cast::{forget, transmute}; +use RawVec = raw::Vec; use clone::Clone; use cmp::{Ord, Eq, Ordering, TotalEq, TotalOrd, max}; use container::{Container, Mutable}; use default::Default; use fmt; use iter::{DoubleEndedIterator, FromIterator, Extendable, Iterator, range}; -use mem::{min_align_of, move_val_init, size_of}; use mem; -use num; use num::{CheckedMul, CheckedAdd}; +use num; use ops::{Add, Drop}; use option::{None, Option, Some, Expect}; use ptr::RawPtr; use ptr; use raw::Slice; -use RawVec = raw::Vec; use rt::heap::{allocate, reallocate, deallocate}; use slice::{ImmutableEqVector, ImmutableVector, Items, MutItems, MutableVector}; use slice::{MutableTotalOrdVector, OwnedVector, Vector}; @@ -91,12 +89,14 @@ impl Vec { /// let vec: Vec = Vec::with_capacity(10); /// ``` pub fn with_capacity(capacity: uint) -> Vec { - if size_of::() == 0 { return Vec { len: 0, cap: ::uint::MAX, ptr: 0 as *mut T } } - if capacity == 0 { + if mem::size_of::() == 0 { + Vec { len: 0, cap: ::uint::MAX, ptr: 0 as *mut T } + } else if capacity == 0 { Vec::new() } else { - let size = capacity.checked_mul(&size_of::()).expect("capacity overflow"); - let ptr = unsafe { allocate(size, min_align_of::()) }; + let size = capacity.checked_mul(&mem::size_of::()) + .expect("capacity overflow"); + let ptr = unsafe { allocate(size, mem::min_align_of::()) }; Vec { len: 0, cap: capacity, ptr: ptr as *mut T } } } @@ -117,7 +117,8 @@ impl Vec { unsafe { let mut xs = Vec::with_capacity(length); while xs.len < length { - move_val_init(xs.as_mut_slice().unsafe_mut_ref(xs.len), op(xs.len)); + mem::move_val_init(xs.as_mut_slice().unsafe_mut_ref(xs.len), + op(xs.len)); xs.len += 1; } xs @@ -133,7 +134,8 @@ impl Vec { /// - there must be `length` valid instances of type `T` at the /// beginning of that allocation /// - `ptr` must be allocated by the default `Vec` allocator - pub unsafe fn from_raw_parts(length: uint, capacity: uint, ptr: *mut T) -> Vec { + pub unsafe fn from_raw_parts(length: uint, capacity: uint, + ptr: *mut T) -> Vec { Vec { len: length, cap: capacity, ptr: ptr } } @@ -212,7 +214,8 @@ impl Vec { unsafe { let mut xs = Vec::with_capacity(length); while xs.len < length { - move_val_init(xs.as_mut_slice().unsafe_mut_ref(xs.len), value.clone()); + mem::move_val_init(xs.as_mut_slice().unsafe_mut_ref(xs.len), + value.clone()); xs.len += 1; } xs @@ -405,16 +408,19 @@ impl Container for Vec { #[inline(never)] unsafe fn alloc_or_realloc(ptr: *mut T, size: uint, old_size: uint) -> *mut T { if old_size == 0 { - allocate(size, min_align_of::()) as *mut T + allocate(size, mem::min_align_of::()) as *mut T } else { - reallocate(ptr as *mut u8, size, min_align_of::(), old_size) as *mut T + reallocate(ptr as *mut u8, size, + mem::min_align_of::(), old_size) as *mut T } } #[inline] unsafe fn dealloc(ptr: *mut T, len: uint) { - if size_of::() != 0 { - deallocate(ptr as *mut u8, len * size_of::(), min_align_of::()) + if mem::size_of::() != 0 { + deallocate(ptr as *mut u8, + len * mem::size_of::(), + mem::min_align_of::()) } } @@ -494,11 +500,14 @@ impl Vec { /// assert_eq!(vec.capacity(), 11); /// ``` pub fn reserve_exact(&mut self, capacity: uint) { - if size_of::() == 0 { return } + if mem::size_of::() == 0 { return } + if capacity > self.cap { - let size = capacity.checked_mul(&size_of::()).expect("capacity overflow"); + let size = capacity.checked_mul(&mem::size_of::()) + .expect("capacity overflow"); unsafe { - self.ptr = alloc_or_realloc(self.ptr, size, self.cap * size_of::()); + self.ptr = alloc_or_realloc(self.ptr, size, + self.cap * mem::size_of::()); } self.cap = capacity; } @@ -513,7 +522,8 @@ impl Vec { /// vec.shrink_to_fit(); /// ``` pub fn shrink_to_fit(&mut self) { - if size_of::() == 0 { return } + if mem::size_of::() == 0 { return } + if self.len == 0 { if self.cap != 0 { unsafe { @@ -523,9 +533,12 @@ impl Vec { } } else { unsafe { - // Overflow check is unnecessary as the vector is already at least this large. - self.ptr = reallocate(self.ptr as *mut u8, self.len * size_of::(), - min_align_of::(), self.cap * size_of::()) as *mut T; + // Overflow check is unnecessary as the vector is already at + // least this large. + self.ptr = reallocate(self.ptr as *mut u8, + self.len * mem::size_of::(), + mem::min_align_of::(), + self.cap * mem::size_of::()) as *mut T; } self.cap = self.len; } @@ -568,25 +581,26 @@ impl Vec { /// ``` #[inline] pub fn push(&mut self, value: T) { - if size_of::() == 0 { + if mem::size_of::() == 0 { // zero-size types consume no memory, so we can't rely on the address space running out self.len = self.len.checked_add(&1).expect("length overflow"); - unsafe { forget(value); } + unsafe { mem::forget(value); } return } if self.len == self.cap { - let old_size = self.cap * size_of::(); - let size = max(old_size, 2 * size_of::()) * 2; + let old_size = self.cap * mem::size_of::(); + let size = max(old_size, 2 * mem::size_of::()) * 2; if old_size > size { fail!("capacity overflow") } unsafe { - self.ptr = alloc_or_realloc(self.ptr, size, self.cap * size_of::()); + self.ptr = alloc_or_realloc(self.ptr, size, + self.cap * mem::size_of::()); } self.cap = max(self.cap, 2) * 2; } unsafe { let end = (self.ptr as *T).offset(self.len as int) as *mut T; - move_val_init(&mut *end, value); + mem::move_val_init(&mut *end, value); self.len += 1; } } @@ -644,7 +658,7 @@ impl Vec { #[inline] pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { unsafe { - transmute(Slice { data: self.as_mut_ptr() as *T, len: self.len }) + mem::transmute(Slice { data: self.as_mut_ptr() as *T, len: self.len }) } } @@ -664,10 +678,10 @@ impl Vec { #[inline] pub fn move_iter(self) -> MoveItems { unsafe { - let iter = transmute(self.as_slice().iter()); + let iter = mem::transmute(self.as_slice().iter()); let ptr = self.ptr; let cap = self.cap; - forget(self); + mem::forget(self); MoveItems { allocation: ptr, cap: cap, iter: iter } } } @@ -949,7 +963,7 @@ impl Vec { ptr::copy_memory(p.offset(1), &*p, len - index); // Write it in, overwriting the first copy of the `index`th // element. - move_val_init(&mut *p, element); + mem::move_val_init(&mut *p, element); } self.set_len(len + 1); } @@ -1395,7 +1409,7 @@ impl Vector for Vec { /// ``` #[inline] fn as_slice<'a>(&'a self) -> &'a [T] { - unsafe { transmute(Slice { data: self.as_ptr(), len: self.len }) } + unsafe { mem::transmute(Slice { data: self.as_ptr(), len: self.len }) } } } @@ -1538,7 +1552,7 @@ impl FromVec for ~[T] { // as it still needs to free its own allocation. v.set_len(0); - transmute(ret) + mem::transmute(ret) } } } -- cgit 1.4.1-3-g733a5