Skip to content

Commit

Permalink
clarify safety in RawVec::into_box
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 5, 2020
1 parent 7b657d3 commit 6cbe172
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,19 @@ impl<T> RawVec<T, Global> {
///
/// # Safety
///
/// `shrink_to_fit(len)` must be called immediately prior to calling this function. This
/// implies, that `len` must be smaller than or equal to `self.capacity()`.
/// * `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);
// NOTE: not calling `capacity()` here; actually using the real `cap` field!
let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
Box::from_raw(slice)
}
Expand Down

0 comments on commit 6cbe172

Please sign in to comment.