diff options
| author | bors <bors@rust-lang.org> | 2014-04-18 11:41:23 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-04-18 11:41:23 -0700 |
| commit | d1d8497e53af86687e701eea0fe2a41b0c4313eb (patch) | |
| tree | ac2ffc0405647555976c3b6b90e98a4e9398eb11 /src/libstd/vec.rs | |
| parent | ce2bab68d69ee04e17c0165dbdb7b583d5a7c991 (diff) | |
| parent | 675b82657e7d9fd4c824ff3c6dbead1edd1ab515 (diff) | |
| download | rust-d1d8497e53af86687e701eea0fe2a41b0c4313eb.tar.gz rust-d1d8497e53af86687e701eea0fe2a41b0c4313eb.zip | |
auto merge of #13588 : alexcrichton/rust/no-more-growing, r=thestinger
This is all in preparation for DST. This removes all the growable/shrinkable methods from `~[T]`.
Diffstat (limited to 'src/libstd/vec.rs')
| -rw-r--r-- | src/libstd/vec.rs | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 96cbac8869e..5480805478c 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -614,13 +614,9 @@ impl<T> Vec<T> { /// ``` #[inline] pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { - // See the comment in as_slice() for what's going on here. - let slice = if mem::size_of::<T>() == 0 { - Slice { data: 1 as *T, len: self.len } - } else { - Slice { data: self.ptr as *T, len: self.len } - }; - unsafe { transmute(slice) } + unsafe { + transmute(Slice { data: self.as_mut_ptr() as *T, len: self.len }) + } } /// Creates a consuming iterator, that is, one that moves each @@ -1143,7 +1139,15 @@ impl<T> Vec<T> { /// would also make any pointers to it invalid. #[inline] pub fn as_ptr(&self) -> *T { - self.as_slice().as_ptr() + // If we have a 0-sized vector, then the base pointer should not be NULL + // because an iterator over the slice will attempt to yield the base + // pointer as the first element in the vector, but this will end up + // being Some(NULL) which is optimized to None. + if mem::size_of::<T>() == 0 { + 1 as *T + } else { + self.ptr as *T + } } /// Returns a mutable unsafe pointer to the vector's buffer. @@ -1155,7 +1159,12 @@ impl<T> Vec<T> { /// would also make any pointers to it invalid. #[inline] pub fn as_mut_ptr(&mut self) -> *mut T { - self.as_mut_slice().as_mut_ptr() + // see above for the 0-size check + if mem::size_of::<T>() == 0 { + 1 as *mut T + } else { + self.ptr + } } /// Retains only the elements specified by the predicate. @@ -1356,16 +1365,7 @@ impl<T> Vector<T> for Vec<T> { /// ``` #[inline] fn as_slice<'a>(&'a self) -> &'a [T] { - // If we have a 0-sized vector, then the base pointer should not be NULL - // because an iterator over the slice will attempt to yield the base - // pointer as the first element in the vector, but this will end up - // being Some(NULL) which is optimized to None. - let slice = if mem::size_of::<T>() == 0 { - Slice { data: 1 as *T, len: self.len } - } else { - Slice { data: self.ptr as *T, len: self.len } - }; - unsafe { transmute(slice) } + unsafe { transmute(Slice { data: self.as_ptr(), len: self.len }) } } } |
