diff options
| author | bors <bors@rust-lang.org> | 2020-06-08 16:32:49 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-06-08 16:32:49 +0000 |
| commit | bc10b68e798477066d4b1ec4886a3b1cdc4feb7e (patch) | |
| tree | 1c2d58cb9bd0d9a45bc5972c256eac6db0f94a14 /src/liballoc | |
| parent | 73558160933b2764ed9a84b1b2b647e128eac3f8 (diff) | |
| parent | 7983e56f40e7536a645993485754c302e5090435 (diff) | |
| download | rust-bc10b68e798477066d4b1ec4886a3b1cdc4feb7e.tar.gz rust-bc10b68e798477066d4b1ec4886a3b1cdc4feb7e.zip | |
Auto merge of #73115 - RalfJung:rollup-jecowhz, r=RalfJung
Rollup of 10 pull requests Successful merges: - #72026 (Update annotate-snippets-rs to 0.8.0) - #72583 (impl AsRef<[T]> for vec::IntoIter<T>) - #72615 (Fix documentation example for gcov profiling) - #72761 (Added the documentation for the 'use' keyword) - #72799 (Add `-Z span-debug` to allow for easier debugging of proc macros) - #72811 (Liballoc impl) - #72963 (Cstring `from_raw` and `into_raw` safety precisions) - #73001 (Free `default()` forwarding to `Default::default()`) - #73075 (Add comments to `Resolve::get_module`) - #73092 (Clean up E0646) Failed merges: r? @ghost
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/raw_vec.rs | 50 | ||||
| -rw-r--r-- | src/liballoc/vec.rs | 39 |
2 files changed, 47 insertions, 42 deletions
diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index 56e284a12fa..5b365f0387a 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -118,6 +118,30 @@ impl<T> RawVec<T, Global> { RawVec::from_raw_parts(slice.as_mut_ptr(), slice.len()) } } + + /// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`. + /// + /// Note that this will correctly reconstitute any `cap` changes + /// that may have been performed. (See description of type for details.) + /// + /// # Safety + /// + /// * `len` must be greater than or equal to the most recently requested capacity, and + /// * `len` must be less than or equal to `self.capacity()`. + /// + /// Note, that the requested capacity and `self.capacity()` could differ, as + /// an allocator could overallocate and return a greater memory block than requested. + pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>]> { + // Sanity-check one half of the safety requirement (we cannot check the other half). + debug_assert!( + len <= self.capacity(), + "`len` must be smaller than or equal to `self.capacity()`" + ); + + let me = ManuallyDrop::new(self); + let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len); + Box::from_raw(slice) + } } impl<T, A: AllocRef> RawVec<T, A> { @@ -520,32 +544,6 @@ where Ok(memory) } -impl<T> RawVec<T, Global> { - /// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`. - /// - /// Note that this will correctly reconstitute any `cap` changes - /// that may have been performed. (See description of type for details.) - /// - /// # Safety - /// - /// * `len` must be greater than or equal to the most recently requested capacity, and - /// * `len` must be less than or equal to `self.capacity()`. - /// - /// Note, that the requested capacity and `self.capacity()` could differ, as - /// an allocator could overallocate and return a greater memory block than requested. - pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>]> { - // Sanity-check one half of the safety requirement (we cannot check the other half). - debug_assert!( - len <= self.capacity(), - "`len` must be smaller than or equal to `self.capacity()`" - ); - - let me = ManuallyDrop::new(self); - let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len); - Box::from_raw(slice) - } -} - unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> { /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. fn drop(&mut self) { diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 96923ea47f3..af943ecfd48 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1906,6 +1906,22 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> { //////////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] +impl<T> ops::Deref for Vec<T> { + type Target = [T]; + + fn deref(&self) -> &[T] { + unsafe { slice::from_raw_parts(self.as_ptr(), self.len) } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<T> ops::DerefMut for Vec<T> { + fn deref_mut(&mut self) -> &mut [T] { + unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone> Clone for Vec<T> { #[cfg(not(test))] fn clone(&self) -> Vec<T> { @@ -1961,22 +1977,6 @@ impl<T, I: SliceIndex<[T]>> IndexMut<I> for Vec<T> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T> ops::Deref for Vec<T> { - type Target = [T]; - - fn deref(&self) -> &[T] { - unsafe { slice::from_raw_parts(self.as_ptr(), self.len) } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<T> ops::DerefMut for Vec<T> { - fn deref_mut(&mut self) -> &mut [T] { - unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl<T> FromIterator<T> for Vec<T> { #[inline] fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> { @@ -2628,6 +2628,13 @@ impl<T> IntoIter<T> { } } +#[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")] +impl<T> AsRef<[T]> for IntoIter<T> { + fn as_ref(&self) -> &[T] { + self.as_slice() + } +} + #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<T: Send> Send for IntoIter<T> {} #[stable(feature = "rust1", since = "1.0.0")] |
